简体   繁体   中英

Object defined but when accessing a property returns undefined

I have a really weird issue, can someone explain please. In my controller, when I am logging an object

console.log($rootScope.authUser)

it returns:

在此处输入图片说明

where messages is an array of objects. When I am trying to access one of the properties though:

console.log($rootScope.authUser.messages)

I am getting an empty array - different results! How is this possible 0.o

Try to use

console.log(angular.copy($rootScope.authUser))

It seems that console.log prints the complete object reference, and at the time you look at it, it has already changed. If you make a copy, the copy will not be changed further and you got the "real" output at that time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM