javascript - Why isn't $scope updated when bound properties of services are updated by other services? -


i have 1 service handling data, , 1 logic.

app.service('dataservice', function() {   this.stuff = [false];   this.setstuff = function(s){     this.stuff = angular.copy(s);   } }); 

the data service has set function , data property.

app.service('logicservice', function(dataservice, $http) {   dataservice.setstuff(["apple", "banana"]);   $http.get("./data.json").then(function(res){     dataservice.setstuff(res.data.stuff);   });  }); 

i assigning property of data service controller binding dom.

app.controller('mainctrl', function($scope, dataservice, logicservice  ) {   $scope.message = "hello, world!";   $scope.stuff = dataservice.stuff;    //this way work, isn't janky?   //$scope.$watch(   //  function(){   //    return dataservice.stuff   //  },   // function(n,o){   //   $scope.stuff = n;   // })  }) 

if 'seed' data service when logic service instantiated, , later update following $http call, dom reflects 'seeded' or initial value, not update.

is there fundamental missing in understanding of digest loop?

if add $watch function in controller, well, seems yucky.

//fixed//

@scott-schwalbe 's method of using object.asign() works nicely, preserves original structure, , 1 line.

this.setstuff = function(s){    object.assign(this.stuff, s); } 

working plunker

(sorry titlegore)

if data property object , binded scope, scope update whenever object changes long don't dereference (eg data = x). reassigning data object on $http call?

an alternative current code keep reference using object.assign

app.service('dataservice', function() {   this.stuff = [false];   this.setstuff = function(s){     object.assign(this.stuff, s);   } }); 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -