自定義指定
- angular.module('dir',[ ]).directive( 'mydir' ,function( ){ return {template:'<div>hello</div>'}} )
- 自定義指定對原有html的屬性擴展齐鲤。
- 指定的應用:
-
<div mydir> </div>
寫成自定義屬性 ----A----屬性
-<mydir></mydir>
寫成元素昌渤。注意如果需要改變模板路徑里面的內容,就需要使用ng-transclude
,然后在模板指令的配置項里寫
return {templateUrl :../template.html,transclued:true}
-----E----元素 - 可以在配置項里寫
templateUrl(scope:{footer:@footer})
(注意@footer中的元素標簽里面的footer是屬性),
-
Error: $compile:iscp
Invalid Isolate Scope Definition
Description
When declaring isolate scope the scope definition object must be in specific format which starts with mode character (@&=<), after which comes an optional ?, and it ends with an optional local name.
myModule.directive('directiveName', function factory() {
return {
...
scope: {
'localName': '@', // OK
'localName2': '&attr', // OK
'localName3': '<?attr', // OK
'localName4': ' = attr', // OK
'localName5': ' =*attr', // OK
'localName6': 'attr', // ERROR: missing mode @&=<
'localName7': 'attr=', // ERROR: must be prefixed with @&=<
'localName8': '=attr?', // ERROR: ? must come directly after the mode
'localName9': '<*' // ERROR: * is only valid with =
}
...
}
});
指令中多個單詞采用駝峰命名法('myDir')阶淘。在html中可以使用my-dir來寫指令未桥,因為html不區(qū)分大小寫笔刹。
-- templateUrl(scope:{footer:@footer}届良,replace:true)中的replace:true啊奄,可以替換掉自定義指定形成的標簽蟀苛。指定的類的形式:<div class='mydir'></div>,但是因為默認是A||E,所以要加一個匹配模式配置項:
templateUrl(scope:{footer:@footer}, restrict:C)就可以添加成功稳吮。 ------C類------總共有四種匹配模式:A,C,E,M(注釋,比較少用)蛮寂,如果不寫匹配模式莉钙,默認為A || E.