【问题标题】:Simple isolate scope in directive not working指令中的简单隔离范围不起作用
【发布时间】:2014-09-16 17:21:15
【问题描述】:

我在使用 Angular 1.2.24(最新稳定版本)的简单隔离范围时遇到问题。

app.directive('myDirective', function() {
return {
    restrict: 'A',
    scope: {},
    link: function(scope) {
         scope.name = 'This is my directive';   
    }
};

});

<div my-directive>{{ name }}</div>

但是name 是空的。如果我删除 scope: {} 它可以工作。这是为什么呢?

http://jsfiddle.net/98f97cyt

【问题讨论】:

标签: angularjs angularjs-directive


【解决方案1】:

这样使用

HTML:

<div ng-app="MyModule">

    <div my-directive>

    </div>
</div>

JS:

angular.module('MyModule', []).directive('myDirective', function() {
    return {
        restrict: 'A',
        scope: {},      
        template:'<div>{{ name }}</div>',
        link: function(scope) {
             scope.name = 'This is my directive';   
        }
    };
});

【讨论】:

    【解决方案2】:

    我找到了答案:AngularJS Scope differences between 1.0.x and 1.2.x

    此行为是有意的重大更改,在 Angular 1.2.0(rc3 之后)中引入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多