Strange behaviour with angularJS $scope.$parent

时间:2015-10-06 09:02:23

标签: javascript angularjs angularjs-scope

I'm have having some trouble accessing a value from the parent scope, and what appears to be some strange behaviour also.

If I log $scope.$parent to the console and inspect the DOM object there is a property topicDiscovery.name. However, when I try to log this ($scope.$parent.topicDiscovery.name) it returns undefined.

Also, when I try to log the topicDiscovery object it returns an empty array, even though when its not empty in the DOM for $scope.$parent.

Why is this?

1 个答案:

答案 0 :(得分:-1)

这意味着$scope.$parent.topicDiscovery之后console.log会发生变化。当您执行console.log时,Google Chrome不会打印所有json对象。它只是引用当前的Object。尝试使用JSON.stringify($scope.$parent),这里你不会得到这个属性,因为stringify将JS对象转换为字符串,并且在orignal对象和字符串之间不会有任何内存链接。

这是最好的例子。



var d={z:{b:{}}};

console.log(d); //When you check here, d.a_1 is available
console.log("d.a_1",d.a); //Here d.a_1 is not available.

//Here I am adding d.a_1 property
for(var i=0;i<10;i++){
  d['a_'+i]=i;
}
&#13;
&#13;
&#13;