This time I will show you how to use angular's custom instructions. What are the precautions when using angular's custom instructions. The following is a practical case, let's take a look.
Customized specification
angular.module('dir',[ ]).directive( 'mydir' ,function( ){ return {template:'<div>hello</div>'}} )
Customized specification extends the attributes of the original html.
Specified application:
<div mydir> </div>写成自定义属性 ----A----属性 -<mydir></mydir>写成元素。注意如果需要改变模板路径里面的内容,就需要使用ng-transclude,然后在模板指令的配置项里写 return {templateUrl :../template.html,transclued:true} -----E----元素
You can write templateUrl(scope:{footer:@footer}) in the configuration item (note that footer in the element tag in @footer is an attribute ),
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 = } ... } });
Multiple words in the directive use camel case naming ('myDir'). You can use my-dir to write instructions in html, because html is not case-sensitive.
-- replace: true in templateUrl(scope:{footer:@footer}, replace:true) can replace the custom-specified tag.
The form of the specified class:
, but because the default is A||E, a matching mode configuration item needs to be added:There are four matching modes in total: A, C, E, M (Comment , less commonly used), if not Write the matching pattern, the default is A || E.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
$watch, $apply and $digest data binding process detailed explanation
javascript must-have detailed explanation
What are the built-in objects of js
The above is the detailed content of How to use angular's custom directives. For more information, please follow other related articles on the PHP Chinese website!