加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:无法从指令中访问范围

发布时间:2020-12-17 17:52:39 所属栏目:安全 来源:网络整理
导读:model.data包含以下内容: { "name": "Jamie","age": 25} 我的指令看起来像: my-directive data="model.data"/my-directive 我已将该指令定义如下: app.directive('myDirective',function(){ return { restrict: 'E',scope: { data: '=' },templateUrl: 'g
model.data包含以下内容:

{
    "name": "Jamie","age": 25
}

我的指令看起来像:

<my-directive data="model.data"></my-directive>

我已将该指令定义如下:

app.directive('myDirective',function(){  
    return {
        restrict: 'E',scope: {
            data: '='
        },templateUrl: 'grid.html',controller: function($scope) {
            console.log($scope);
            console.log($scope.data);
        }
    }
}

问题是console.log($scope)返回$scope中的值.我可以看到它包含数据:

{
    $$asyncQueue: Array[0],$$childHead: null,...
    ...
    data: Array[1]
}

但是,console.log($scope.data)返回undefined.有什么线索的原因?

解决方法

像console.log($scope)这样的任何时候;有效,但是console.log($scope.someProperty);不是,这是因为someProperty被设置为解析promise(例如$http调用或使用$q创建的promise).

第一个日志似乎有效,当实际发生的情况是,当您手动查看日志条目时(即,单击以展开其内容),该值已被填充/解析.

在您的控制器中,您需要$watch属性,并设置监视回调函数,以便在数据到达/解析后执行您想要对数据执行的操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读