使用$ scope。$ parent时“未定义”,但它有数据

时间:2016-02-16 07:11:54

标签: angularjs

我正在尝试访问$ scope。$ parent中的数据。在我尝试访问时,我总是得到未定义但$ scope的日志。$ parent有数据。

例如,

app.controller('ParentCtrl', function($scope){
    $scope.parentData = {
        name : 'Beacon',
        address : 'Cheese'
    };
});

app.controller('ChildCtrl', function($scope){
    console.log($scope.$parent); // There is parentData in log
    console.log($scope.$parent.parentData); // it show 'undefined'
});

我不明白我做错了什么。非常感谢你的帮助。

修改 这是console.log捕获的屏幕($ scope。$ parent); enter image description here

1 个答案:

答案 0 :(得分:0)

我可以看到你的console.log输出如下

console.log($scope.$parent); 
Object { $id: "004", this: Object, $$listeners: Object, $$listenerCount: Object, $parent: Object, $$childTail: Object, $$childHead: Object, $$nextSibling: null, $$watchers: null, $$prevSibling: null, 1 more… } index.html:16:4


console.log($scope.$parent.parentData); 
 Object { name: "Beacon", address: "Cheese" }

请将您的HTMl粘贴到包含控制器的位置。