javascript - Private variables in the controller scope -


in angular, following demos, can define controller as:

function todoctrl($scope) {     $scope.todos = [                     {text:'learn angular', done:true},                     {text:'build angular app', done:false},                     {text:'empty dishwasher', done:false}];      $scope.oldtodos = [];     var oldtodos2 = [];     ... 

you'll note have 2 oldtodos. 1 on $scope , 1 local var. think latter approach way go if want encapsulate variable - i.e. no-one other controller has interest in , former if want pass variable model

am correct?

it looks want keep private copy of oldtodos around in event have either refer them or resurrect 1 of them or something. in case, makes sense not put oldtodos scope. maxim said, scope relevant if want bind values view. if want save oldtodos keep reference them, normal variable fine.

then, if want bring 1 of them back, copy $scope.todos , comes.

note can abstract of todos service , inject service controller level of encapsulation , better testing strategy. enable share todos across controllers if that's necessary.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)