data binding - View updates everywhere but in current controller - angularJS -


edit: asked, i'll explain bit more efficiently !

i've been sitting in front of annoying problem recently, whenever update value inside directive, controllers i'm not "in" ones updated properly.

scenario example: profile page made of 2 controllers. navbar_controller displaying user name :

<div ng-if="auth.isauthenticated">hello, {{auth.getcurrentuser().name}}</div> 

the second controller , profile_controller here update user values. simple function in angular first controller, updates currentuser:

$scope.updateuser = function (type, form) {     if (!$scope.modif)         return ;      $http.put('/api/users/' + auth.getcurrentuser()._id + '/update', {type:type, modif:$scope.modif})           .success(function (data, status) {                 $scope.user = auth.setnewuser(data);             })             .error(function () {                 console.log("error");             });     }; 

when update, example, name. can see database has been modified properly. , indeed, navbar_controller got update because new name printed in div. however, profile_controller doesn't update: name printed in profile page didn't change.

here 2 basic functions in auth.service.js :

getcurrentuser: function() {     return currentuser;   },  // 'user' data retrieved in http put request dot success setnewuser: function(user) {       currentuser = user;       $rootscope.$broadcast(); // navbar_controller updated or without line       return currentuser;   } 

anyway, if @ navbar , controller, calling auth.getcurrentuser() method, user values instantly modified. i'e been using ugly method consisting in modifying controller values manually or refreshing page... isn't way go, right ?

there must "$rootscope.$broadcast();", i'm new angular , other questions on stackoverflow specific me understand properly.

thank !

your question little difficult understand, think problem reference changing object in various controllers. here example explain:

service:

var myobject = { ... }; return {      getobject() { return myobject; }     setobject(obj) { myobject = obj; } }; 

controller 1:

$scope.myobja = service.getobject(); 

controller 2:

$scope.myobjb = service.getobject(); 

now on initialisation both controllers referencing same object, if changed property inside either controller (eg. $scope.myobjb.name = 'bob';), other controller see name.

however if changed object in controller (eg. service.setobject(newobj);), controller referencing new object, while other controller still referencing old one.

you can fix wrapping service object in container:

var cont = {     user: ... };  function getcontainer() { return cont; }  function setnewuser(user) { cont.user = user; } 

then inside controllers, container (not user):

$scope.cont = service.getcontainer(); 

and inside html:

<div>{{cont.user.name}}</div> 

now when update user, attached controllers updated.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -