Denis,
Its because $watch is only executed when there is a change in value for the
give variable to watch, in this case the ng-init gets executed after the
controller (by that time your log of x is already executed) where the value
is change there by you see myData as `world`. Simply put `x` and
`$scope.myData` are two different variables where `x` is being updated to
`$scope.myData` on in case of value change of `myData` using the $watch
which is invoked after the controller is initialized by the time your log
for x is executed it still undefined as `$scope.myData` is not yet
defined/change this happens after when the ng-init is executed. As far as
my understanding goes the controller is executed (to initialize the
necessary scope) first and later the directive (in this case ng-init). I
hope this clarifies it a bit.
On Tuesday, 11 March 2014 22:06:05 UTC+5:30, Denis Ipatov wrote:
>
> I made another experimet in code:
>
> var app = angular.module("app", []);
>
> app.controller("appCtrl", function ($scope) {
> var x;
> $scope.$watch('myData', function () {
> if ($scope.myData) {
> //here here you can handle your data
> x = $scope.myData;
> console.log('myData:', $scope.myData);
> }
> });
> console.log('my x:', x);
>
> Result is undefined. Why?
>
> my x: undefined app.js:12 <http://localhost:7007/Scripts/app.js>
> myData: world
>
>
> Hi Denis,
>>
>> No that's not the only way, but it's an easy way to get started,
>> What do you know already of angular?
>> you already know something about modules and dependency injection ?
>>
>> here is another plunk,
>> http://plnkr.co/edit/eauguSJOoATrATATwn6E?p=preview
>> if you have questions about it, don't hesitate to ask!
>>
>> Regards
>> Sander
>>
>>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.