Reputation: 701
I have been start learning AngularJS 1.4.7 and I can't understand completely the $scope variables. Is this a variable inside a Angular controller "like" public variables inside a PHP class?
For example:
I have 2 functions function1(), function2()
in HomeCtrl.js
and a $scope.img = null;
variable, and 2 ng-click event in template file to call these functions.
On ng-cick
event i call the function1
and set the value of $scope.url
to somethig
Thereafter i call the function2
and console.log($.scope.url)
it retutn null. Null is the initialized value of this varaiable.
I'v just set the value of $scope.url
in function1
and inside function2
value of variabel is null, why?
I have no idea why happening this.
Upvotes: 0
Views: 111
Reputation: 449
Do you literally call $.scope.url
? Then that's a problem, it should be $scope.url
, no dot between "$" and "scope".
Scope variables have nothing to do with variables accessibility concept (public/private/protected/etc).
They are those "magic" variables, which can be placed inside templates using expressions, which allows data-binding in Angular, i.e., your html with that "magic" scope variables, gets automatically updated by angular, when you just change their values inside your controller, like in that callback function for ng-click. (Actually it's not that easy, here we come to $watch and $digest, but if you are not writing your custom directives, and not mixing, for example, jQuery code with Angular code, you may not think about it for now).
Two-side data-binding, automatic html updates - that's the main point of $scope and $scope variables.
Scope is not and instance of a controller, they are different Angular entities.
Upvotes: 0
Reputation: 25352
scope
is instance of controller
.
When you declare a controller
, scope
will be avaiable there.
You can access scope through out that controller
.
That's why when you change on place in a controller
, change will be reflect everywhere in that scope
.
Upvotes: 1