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

前端之家收集整理的这篇文章主要介绍了AngularJS:无法从指令中访问范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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属性,并设置监视回调函数,以便在数据到达/解析后执行您想要对数据执行的操作.

猜你在找的Angularjs相关文章