自定義指令

2021-09-06 19:32:21 字數 830 閱讀 8826

自定義指令有兩種方法:

$complieprovider.directive(''指令名',function(){

return {

restrict: 'acem',

replace:true,

transclude:true,

template:'content

',return {

restrict: 'acem',

replace:true,

transclude:true,

template:'content

',templateurl:'demo/index.html' //這裡的路徑是相對angular下的index.html的,模板裡的內容可以獲取控制器裡定義的屬性變數

指令常用配置屬性

priority //設定指令的優先順序,值越大優先順序越高

terminal:true//這個屬性必須和priority共用,過濾所有比priority值低的指令

transclude //這個屬性可以保留被替換的內容,如在新模板裡加上這個就會將原始資料載入到這個div裡

link

compile

還有一種在當前檢視裡載入另乙個模板的方式

//這裡將用customtag2模板裡的內容來替換,必須用分號隔開

return {

restrict: 'acem',

replace:true,

transclude:true,

templateurl:'customtag2' //這裡的路徑是相對angular下的index.html的,模板裡的內容可以獲取控制器裡定義的屬性變數

自定義指令

像v if,v show等,是vue已經內建的指令,我們也可以使用directive來自定義指令 其中的blue要被引號所包裹,不然會被識別為變數來進行查詢全域性自定義指令 vue.directive focus vue.directive color var vm new vue 簡寫形式,代表我...

自定義指令

directives.js 存放 自定義標籤 strict require scope link 接收兩個引數 第乙個引數表示自定義指令的名稱 在頁面中使用 分隔單詞,在自定義指令中通過駝峰式命名 第二個引數表示自定義指令處理函式 返回值是乙個物件,用來描述我們自定義指令的 描述自定義指令的物件有一...

自定義指令

vue中有內建指令v model等 還可自定義指令 全域性指令 vue.directive focus 指令繫結時執行 inserted function el updated function 更新時執行 鉤子函式的第乙個引數指定為繫結該指令的dom元素區域性指令 var vm newvue js...