Can someone explain the different between two variables a1
and a2
:
app.controller("someCtrl",function(){
this.a1=somevalue;
var a2=somevalue;
});
And what lifetime a2
has?
Can someone explain the different between two variables a1
and a2
:
app.controller("someCtrl",function(){
this.a1=somevalue;
var a2=somevalue;
});
And what lifetime a2
has?
They are similar. Same lifetime however this.a1 has model bindings (which is fundamental within angular applications if you're going to use this variable anywhere in your view) where as a2 doesn't and you won't be able to access it using $scope within the view.
Simply speaking, this.a1
will create a property called a1
on whatever object this
refers to when invoking the controller function. Since you are invoking the constructor function, this
will refer to the controller itself, so you will be able to use a1
for data-binding. var a2
will create a local variable called a2
inside the function.