【问题标题】:AngularJS isolated directives inheriting surrounding or parent scopeAngularJS 隔离指令继承周围或父范围
【发布时间】:2014-05-20 09:56:06
【问题描述】:

我已经把我的代码放在了 jsbin:http://jsbin.com/fewom/1/edit

如果有人可以指导我,我在那里做错了什么。我已经在 myDirective 中指定了要隔离的范围,范围为:{} 但是当我在 html 中的指令中写入 myProperty 时,我仍然能够读取 myProperty。

我使用的是 AngularJS https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js

当我将我的库更改为它开始工作时,

https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js

那我试过了,

Angular JS 版本 1.2.16 https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js

Angular JS 版本 1.3.0-beta.5 https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.5/angular.min.js

孤立作用域的问题再次出现。

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    您没有控制器,因此没有任何东西将您的指令与您的 html 联系起来。

    把你的js改成这个(我只是加了一个空白控制器):

    angular.module('myApp', [])
        .directive('myDirective', function() {
            return {
                restrict: 'A',
                scope: {}
            };     
        })
    .controller('ctrl', function (){
    
    });
    

    在你的 html 中,改成这个(引用控制器):

    <html ng-app="myApp" ng-controller='ctrl'>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2015-12-08
    相关资源
    最近更新 更多