简体   繁体   中英

How to bind a custom directive attribute value to an isolated scope in angular

    app.directive('myCustomAttr',function() {
    return {
        restrict: 'A',
        scope: {
            valueOfAttr : "@myCustomAttr"
        },
      };
});

How would I pass the value of the attribute? Thus far, I've only found examples that use restrict : 'E'.

<input type="text" my-custom-attr="myValue" />

So, if I were going to bind "myValue" to the scope, how would I do that?

[EDIT]

Sorry, I had a typo. I was using the my-custom-attribute correctly, but it still doesn't seem to bind in the directive.

I'm pretty sure you want =myCustomAttr instead of @myCustomAttr . There's mention of this under "Isolating the Scope of a Directive" . There's also more information here .

use the link function:

link: function (scope, element, attrs) {
   var myAttr = attrs["myCustomAttr"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM