angularjs - My $scope variable not updated -
i have collection of objects service:
$scope.rooms = testdata.list().rooms; // {"room1":{"name":"bathroom"},"room2":{"name":"wcc"},"room3":{"name":"kitchen"}};
i need find room $routeparams.id
$scope.room = $scope.rooms[$routeparams.id]; // example $routeparams.id = 'room1'
if inspect in view:
{{rooms}} {{room}}
i see how rooms dynamically updated, room undefined
. if call in view this:
{{room = rooms[roomuid]}} // $scope.roomuid = $routeparams.id
it found room expected. why can not find room in controller?
updated:
i found problem, $scope.rooms
collection empty when try find room id. $timeout
$scope.room
work expected, how start finding room after $scope.rooms
populated?
$timeout(function(){$scope.room = $scope.rooms[$routeparams.id];}, 3000);
i solved by:
if($routeparams.id){ $scope.action = 'edit'; $scope.roomid = $routeparams.id; $scope.$watch('rooms.' + $scope.roomid, function(){ $scope.room = $scope.rooms[$routeparams.id]; }); };
it good?
$scope.$watch
not required in case. 1 add $watch
if room dynamically added , @ time want something.
just check $routeparams.id
, if it's not undefined
start searching room
.
in case you're fetching rooms
data service. put logic getting particular room inside then
resolve function. in way sure rooms list available before search particular room.
Comments
Post a Comment