AngularJS directive scope not resolved (“attr name is not defined” error)

前端 未结 2 1614
萌比男神i
萌比男神i 2020-12-21 19:22

Directive code

mymodule.directive(\'eicon\', function(){
    return {
        restrict: \'E\',
        scope: {
            attr: \'=\'
        },
        te         


        
相关标签:
2条回答
  • 2020-12-21 19:44

    attr is accessible in scope, so you can access scope.attr in your controller or linking phase, or {{attr}} in templates. A simple solution is to change your template to

    mymodule.directive('eicon', function(){
        return {
            restrict: 'E',
            scope: {
                attr: '='
            },
            template: "test {{attr.name}}",
            link: function (scope, element, attrs) {
              console.log(scope.attr);
            },
            controller: function (scope) {
              console.log(scope.attr);
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-21 19:45

    Since you are declaring isolated scope property attr you should be able to access scope.attr in template like this:

    mymodule.directive('eicon', function(){
        return {
            restrict: 'E',
            scope: {
                attr: '='
            },
            template: "test {{attr.name}}"
        }
    });
    

    Demo: http://plnkr.co/edit/YZ0aPqhkMlIIwmrkKK2v?p=preview

    0 讨论(0)
提交回复
热议问题