AngularJS Directive Value

后端 未结 1 1231
清歌不尽
清歌不尽 2021-02-19 15:08

I have an AngularJS directive. My directive is restricted to be used as an attribute. However, I want to get the value of the attribute. Currently, I have my attribute defined a

相关标签:
1条回答
  • 2021-02-19 15:53

    You can use the attributes argument of the link function:

    angular.module('myDirectives.color', [])
        .directive('setColor', function () {
            return {
                restrict: 'A',
                link: {
                    pre: function (scope, element, attrs) {
                        console.log('color is: ' + attrs.setColor);
                    }
                }
            };
        });
    

    or you could create an isolate scope like this:

    angular.module('myDirectives.color', [])
        .directive('setColor', function () {
            return {
                restrict: 'A',
                scope: {
                  setColor: '@'
                },
                link: {
                    pre: function (scope) {
                        console.log('color is: ' + scope.setColor);
                    }
                }
            };
        });
    
    0 讨论(0)
提交回复
热议问题