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

前端 未结 2 1613
萌比男神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);
            }
        }
    });
    

提交回复
热议问题