javascript - Updating views on load method + http.get strange behaviour -


i have angularjs application different views. 1 of views used control tool has 3 different states : started, stopped, paused corresponding buttons.

the tool runs on server , want update state of view data server each time go it. example : start tool, go different view, when go on tool view, call service using $http.get obtain state of tool. returns me string "started", , use update elements of view.

1 - better use ng-init="update()" in tool.html update @ each reload in :

<div ng-init=update()> [all content] </div> 

or call update() in tool controller ? or maybe methode suggest me.

2 - tried 2 precedent methods, , in each case have problem sometimes, http.get isn't working , elements not correctly actualised. use way :

$http.get('linktogetthestate')           .then(function(response) {               $scope.toolstatus = response.data;           }) switch($scope.toolstatus) ... 

sometimes works, other time doesn't (when run step step, skips everyting beginning of http.get until end of switch , toolstatus stays undefined), , weirder, works better if call function twice... if has clue on happening here and/or why, thankful.

i hope question clear, i'm new angular/stackoverflow, , can give more precisions if needed.

my problem resolved. here solutions found :

1 - prefer call update() function in controller follow :

app.controller('myctrl', function ($scope, ...) {     [...]      $scope.update = function() {                       ...         });     };      [...]      $scope.update(); }); 

this way, each time corresponding view displayed, function called once update (when changing tabs , when refreshing page).

- 2 part, still don't know problem came from. used method gatsbill adviced me here (in edit part of post) : gatsbill answer clean/reorganize project. , service handle $http request, working fine.


Comments