2

I am new to Angular JS and I have a basic Angular App that simply sets up a scope object, sets up a few functions to initialize/create the scope object. When I use a console.log within my function it is reporting the $scope values that are set at the end of the scope function rather than the values of the scope object at the moment of console.log. I'm sure this is a basic execution order issue and I would appreciate any explanation or help with this issue.

Here is the angular code:

var module = angular.module('my-app', []);

function MainController($scope)
{
    $scope.testDict = {};

    $scope.createNewTestDict = function(){
        $scope.testDict.test1 = {1: false, 2: false, 3: false}
    }

    $scope.initializeDict = function(){
        $scope.createNewTestDict();
        console.log($scope.testDict);
        $scope.testDict.test1 = {1: true, 2: true, 3: true}
    }

    $scope.initializeDict();
}

JSFiddle Here: http://jsfiddle.net/U6ZKr/7/

So you will notice that the console.log in the actual scope object at the time of the log should be all values false, but they are coming out all true which was set after the console.log.

Thanks in advance for your help!

3 Answers 3

8

Since $scope.testDict is passed as a reference to the console.log and it is evaluated asynchronously, the value might not accurate.

One alternative is to use console.log(JSON.stringify($scope.testDict)); to see the right results.

Sign up to request clarification or add additional context in comments.

Comments

1

Seems to just be a peculiarity with the way the dev tools work.

Try logging $scope.testDict.test1 instead, and you'll see the values you want.

My guess is that the contents of the deep objects you log don't get evaluated until you click the little triangle to open them up and look inside. In this case, by that time you inspect it, you've already changed the contents.

1 Comment

Ahhh yes! Thank you, I guess since I've never been logging data that changes so frequently I haven't run into this issue. Good eye!
0

You are making the incorrect assumption that the Chrome debugger console.log the value immediately. It will log it but asynchronously.

You can use Google Chrome's script debugger to step in and see this effect.

1 Comment

I would hope that console.log would eventually log the correct values however at the point in time I made the call to console.log -- I used the answer above and logged the item directly vs the entire dictionary and the values are as I expect. Still interesting if console.log would log the updated values instead of the ones at the origination of the call to console.log -- as Edmar suggested in another answer it could be because of the passing of the reference vs the actual object. Thanks for your time!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.