2016-02-16 18 views
0

我试图访问$ 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

+0

你能显示'console.log($ scope。$ parent)的输出吗? – Manwal

+0

@Manwal我在帖子编辑中加入了它。 –

+0

您是否尝试使用控制器'as'符号而不是使用$ parent? – Nijeesh

回答

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。

相关问题