How do I pass multiple attributes into an Angular.js attribute directive?

后端 未结 5 1008
长发绾君心
长发绾君心 2020-11-30 18:57

I have an attribute directive restricted as follows:

 restrict: \"A\"

I need to pass in two attributes; a number and a function/callback, a

5条回答
  •  旧时难觅i
    2020-11-30 19:25

    You do it exactly the same way as you would with an element directive. You will have them in the attrs object, my sample has them two-way binding via the isolate scope but that's not required. If you're using an isolated scope you can access the attributes with scope.$eval(attrs.sample) or simply scope.sample, but they may not be defined at linking depending on your situation.

    app.directive('sample', function () {
        return {
            restrict: 'A',
            scope: {
                'sample' : '=',
                'another' : '='
            },
            link: function (scope, element, attrs) {
                console.log(attrs);
                scope.$watch('sample', function (newVal) {
                    console.log('sample', newVal);
                });
                scope.$watch('another', function (newVal) {
                    console.log('another', newVal);
                });
            }
        };
    });
    

    used as:

    
    
    

提交回复
热议问题