Angular directive restrict usage

Source: Internet
Author: User

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
  1. VaR APP = Angular. Module ("superhero", [])
  2. App. Directive ("Superman", function (){
  3. Return {
  4. Restrict: "",
  5. Link: function (){
  6. Alert ("I'm working ");
  7. }
  8. };
  9. });

From here, instead of having Superman as an element, let's do a div with Superman as an attribute:

Index.html
  1. <Div ng-APP = "superhero">
  2. <Div Superman> </div>
  3. </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
  1. <Div ng-APP = "superhero">
  2. <Div class = "Superman"> </div>
  3. </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
  1. <Div ng-APP = "superhero">
  2. <! -- Directive: Superman -->
  3. </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
  1. VaR APP = Angular. Module ("superhero", [])
  2. App. Directive ("Superman", function (){
  3. Return {
  4. Restrict: "",
  5. Link: function (){
  6. Alert ("I'm working ");
  7. }
  8. };
  9. });
  10. App. Directive ("Flash", function (){
  11. Return {
  12. Restrict: "",
  13. Link: function (){
  14. Alert ("I'm working ");
  15. }
  16. };
  17. });

Now we shoshould have a div with both "Superman" and "Flash" as attributes

Index.html
  1. <Div ng-APP = "superhero">
  2. <Div Superman flash> </div>
  3. </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 ""

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.