E indicates that the command is an element; A indicates that the command is attribute; C indicates that the command is class; M indicates that the command is watched
Example:
Original post: www. thinkster. IO/angularjs/rep5re7gtm/angularjs-Directive-Restrictions
While it's cool to make a custom element like we did the previous cast, it's actually more common to do things like create custom attributes. these attributes are going to add things like behaviors, and we can do so by using restrict "". "A" is for attribute, "e" is for element. you can then provide a linking function, which is where you will put whatever the behavior is. we're re just going to alert "I'm working" to the user.
Main. js
- VaR APP = Angular. Module ("superhero", [])
- App. Directive ("Superman", function (){
- Return {
- Restrict: "",
- Link: function (){
- Alert ("I'm working ");
- }
- };
- });
From here, instead of having Superman as an element, let's do a div with Superman as an attribute:
Index.html
- <Div ng-APP = "superhero">
- <Div Superman> </div>
- </Div>
Now if we refresh, you'll see the alert saying "I'm working" another restriction we can use is "C" for class. if we change restrict to "C" and refresh without changing anything, we can see that nothing happens. we need to change the directive from an attribute to a class of the div.
Index.html
- <Div ng-APP = "superhero">
- <Div class = "Superman"> </div>
- </Div>
If we refresh now, we'll see "I'm working" alerted again. the last restriction is "M" for comment. if we change restrict to "M" and create a comment starting with "Directive:" and then the name of our direve ve, refresh, and we'll see that it works again.
Index.html
- <Div ng-APP = "superhero">
- <! -- Directive: Superman -->
- </Div>
The "M" restriction is used the least often, usually only for backwards compatibility and for passing markup validations. Typically it's best to add behaviors in attributes so you can stack them.
We'll create another attribute directive, call it "Flash" and set the Linking Function to alert "I'm working faster" and change "Superman" to alert "I'm working stronger" (don't forget to change the "Superman" directive's Restriction back to "")
Main. js
- VaR APP = Angular. Module ("superhero", [])
- App. Directive ("Superman", function (){
- Return {
- Restrict: "",
- Link: function (){
- Alert ("I'm working ");
- }
- };
- });
- App. Directive ("Flash", function (){
- Return {
- Restrict: "",
- Link: function (){
- Alert ("I'm working ");
- }
- };
- });
Now we shoshould have a div with both "Superman" and "Flash" as attributes
Index.html
- <Div ng-APP = "superhero">
- <Div Superman flash> </div>
- </Div>
If we refresh, We'll see "I'm working stronger" and then "I'm working faster"
To recap: "E" is for element, "A" is for attribute, "C" is for class, and "M" is for comment. attributes are going to be the main ones as far as adding behaviors that get used the most. if you don't specify the restrict property it will default to ""