angularjs - keeping the view up to date -


i banging head on brick wall, have been hours , hours....

i have controller gets list of objects stored in pouchdb via factory uses angularjs promise return answer it's asynchronous request outside flow of angularjs app. works fine , returns objects.

i have controller uses external rest api carry out search more objects, on results screen have "add" button adds object pouchdb.

my problem no matter try, tutorials follow, answers read cannot first list of objects update when new objects added.

does have idea should doing here? if it's sensible? there better way of doing this? nested controllers maybe?

any advice gratefully received.

it's impossible tell without knowing more code, common mistake happens not being mindful of different scopes angular creates. can use chrome extension angularjs batarang debug scopes. simple debugging set window property scope in controller (i.e. window.sc1 = $scope;) , check out values in console.

specifically since second controller updating objects doesn't have access list of objects created in other controller unless scope child scope (i.e. html of second controller inside html of first controller). in case if set value on child scope change breaks inheritance , new value not propagated parent, though can manipulate inherited values such arrays , objects modifying existing object (i.e. $scope.objects.push(newobject); or $scope.obj.property = "value", not $scope.obj = newobject;.

the more angular way create service singleton , maintains list of data. can inject controller want , same values , call functions on service.

angular.module("myapp").service("objservice", function() {     this.objects = [];     this.getobjects = function() {         // ... code first list of objects     };     this.updateobjects = function() {         // ... code called second controller update list here     };     this.getobjects(); // objects when first injection occurs );  angular.module("myapp").controller("ctrl1", function($scope, objservice) {     $scope.objservice = objservice; }; 

this plnkr shows inherited list used in ctrl2 until sets value on child scope, breaking inheritance.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -