0

I have the following simple controller in my Angular app

vehicleSearchApp.controller('VehicleSearchCtrl', function ($scope, $location){

     $scope.location = $location;
     $scope.$watch('location.search()', function() {
         $scope.target = ($location.search()).target;
     }, true);

     $scope.changeTarget = function(name) {
         $location.search('target', name);
     }

     console.log($scope.target);
     console.log($scope);

 }

The first console log statement towards the bottom of the controller returns undefined, but the second console log statement returns an object that looks like the following:

enter image description here

I see the 'target' property plus a host of other methods / properties in this object. Why do I get an 'undefined' message in console if I try to print using "console.log($scope.target)"?

2 Answers 2

3

Could be a timing issue. You are console.logging the value of $scope.target right away when it's not being set until the $watch statement fires. Most likely it's set by the time it logs to the console and you expand it but not when it reaches the first log.

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

2 Comments

If I move the console.log($scope.target) statement to after the console.log($scope) statement, I get the same thing. The console.log($scope.target) statement is the last line in the controller. How can I access the variable inside my controller in this case?
It's asyncronously setting the value of $scope.target so it wouldn't matter where in that block you moved it. If you move the console.log($scope.target) to right after the $scope.target = within the $watch you will see it's value logged. So you do have access to it just fine, however it has no value where you are logging it. When you console.log($scope) you are logging a reference to the actual object (not a snapshot of it at that time) so when you see it in the console it is after the $watch has been called asyncronously.
0

It' a timing problem: you see undefined because when the code reaches the log statement, $scope.target is not yet avaible.

You could install the batarang addon to the chrome browser to inspect the value of the $scope elements.

5 Comments

I am bit confused, I am really just trying to bind the passed in value from the URL (in this case the $scope.target variable) to another object in my controller. If it's a timing issue, how does this affect my ability to bind my variable? I've tried putting the console.log statement at the very end of the controller and still get the same undefined message in the console.
You can bind it just fine. If you put {{ target }} in your html template it will display the value of $scope.target as soon as it becomes available.
@MattPileggi is right, you can use it in your HTML template, AngularJS will to all the magic to display it when it becomes avaible
@MattPileggi I understand I can use it in my HTML template, but I am trying to set it as the value to another object in my Angular controller. I tried doing so and the other object comes back as undefined
It sounds like you are actually after a service instead of a controller property. I'd suggest creating something like TargetService with methods for changeTarget() and getTarget(). Your various controllers/directives can inject that and either $watch getTarget for changes or even use $rootScope.$broadcast('target:change',newTargetValue); from the service to notify any listeners that care. Either way, it doesn't sound like it should be part of your controller directly.

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.