Update data in the service [Angular.JS]

Good afternoon.

You have a controller that works with the model, the model is quite voluminous.

function FooCtrl($scope, service) {
 $scope.foo1 = null;
 $scope.foo2 = null;
 $scope.foo3 = null;
............................
 $scope.fooN = null;
}

angular.module('myService', []).
 factory('service', function () {
 var foo1 = 'abc';
...........
})

When data is updated, that is, the person, for example, enters the data in input, and decided to move to another page, it is necessary to maintain state between pages, for this was the selected service.

Now the question is: how to upgrade, not to hang $watch on each variable? Of course, you can hang up a $watch on all $scope and update the entire model, but is this the best solution?
Please advise.
October 3rd 19 at 03:52
1 answer
October 3rd 19 at 03:54
$scope You are using as a model, which is wrong. The model should be an object inside the $scope.
If You use the service as a global object and use it inside the controller in the templates. Make $scope.Model = service.Model and templates to use as a Model.foo1.
Will support the author's commentary.

But I would advise you to keep a separate object for variables.

ie

$scope.Model.vars = service.Model.vars;
$scope.Model.func = service.Model;

I.e. to explicitly share variables between functions. Otherwise it may happen that you override the function, replacing the data.

Clean IMHO. - Rocky_Wilkinson34 commented on October 3rd 19 at 03:57

Find more questions by tags AngularJavaScript