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

后端 未结 5 993
长发绾君心
长发绾君心 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条回答
  • 2020-11-30 19:19

    This worked for me and I think is more HTML5 compliant. You should change your html to use 'data-' prefix

    <div data-example-directive data-number="99"></div>
    

    And within the directive read the variable's value:

    scope: {
            number : "=",
            ....
        },
    
    0 讨论(0)
  • 2020-11-30 19:23

    If you "require" 'exampleDirective' from another directive + your logic is in 'exampleDirective's' controller (let's say 'exampleCtrl'):

    app.directive('exampleDirective', function () {
        return {
            restrict: 'A',
            scope: false,
            bindToController: {
                myCallback: '&exampleFunction'
            },
            controller: 'exampleCtrl',
            controllerAs: 'vm'
        };
    });
    app.controller('exampleCtrl', function () {
        var vm = this;
        vm.myCallback();
    });
    
    0 讨论(0)
  • 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:

    <input type="text" ng-model="name" placeholder="Enter a name here">
    <input type="text" ng-model="something" placeholder="Enter something here">
    <div sample="name" another="something"></div>
    
    0 讨论(0)
  • 2020-11-30 19:40

    The directive can access any attribute that is defined on the same element, even if the directive itself is not the element.

    Template:

    <div example-directive example-number="99" example-function="exampleCallback()"></div>
    

    Directive:

    app.directive('exampleDirective ', function () {
        return {
            restrict: 'A',   // 'A' is the default, so you could remove this line
            scope: {
                callback : '&exampleFunction',
            },
            link: function (scope, element, attrs) {
                var num = scope.$eval(attrs.exampleNumber);
                console.log('number=',num);
                scope.callback();  // calls exampleCallback()
            }
        };
    });
    

    fiddle

    If the value of attribute example-number will be hard-coded, I suggest using $eval once, and storing the value. Variable num will have the correct type (a number).

    0 讨论(0)
  • 2020-11-30 19:41

    You could pass an object as attribute and read it into the directive like this:

    <div my-directive="{id:123,name:'teo',salary:1000,color:red}"></div>
    
    app.directive('myDirective', function () {
        return {            
            link: function (scope, element, attrs) {
               //convert the attributes to object and get its properties
               var attributes = scope.$eval(attrs.myDirective);       
               console.log('id:'+attributes.id);
               console.log('id:'+attributes.name);
            }
        };
    });
    
    0 讨论(0)
提交回复
热议问题