javascript - Angular view not updated -



javascript - Angular view not updated -

if modify $scope after $timeout, view doesn't rendered properly. don't understand why. works if don't $timeout first.

http://plnkr.co/edit/ogupgt7vaykgpfh00ldv?p=preview

<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js'></script> <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script> angular.module('treeapp', []) .controller('treecontroller', ['$scope', '$timeout', function($scope, $timeout) { // works // $scope.nodes = { // 1:{node_id:1, text:'foo text', parent_id:null, child_ids:[2,3]}, // 2:{node_id:2, text:'bar text', parent_id:1, child_ids:[]}, // 3:{node_id:3, text:'abc text', parent_id:1, child_ids:[]} // } // $scope.top_ids = [1] // doesn't work $timeout(function() { $scope.nodes = { 1:{node_id:1, text:'foo text', parent_id:null, child_ids:[2,3]}, 2:{node_id:2, text:'bar text', parent_id:1, child_ids:[]}, 3:{node_id:3, text:'red text', parent_id:1, child_ids:[]} } $scope.top_ids = [1] }, 1000) }]) </script> <div ng-app="treeapp"> <script type="text/ng-template" id="node.html"> <li node_id="{{node_id}}" ng-repeat="node_id in node_ids" ui-tree-node ng-init="node = nodes[node_id]"> {{node.text}} <ol ng-include="'node.html'" ng-init="node_ids = node.child_ids"></ol> </li> </script> <div ng-controller="treecontroller"> <ol ng-include="'node.html'" ng-init="node_ids = top_ids"></ol> <pre>{{top_ids | json}}</pre> <pre>{{nodes | json}}</pre> </div> </div>

<ol ng-include="'node.html'" ng-init="node_ids = top_ids"></ol>

you initialize node_ids top_ids. it's one-time assignment, not binding. when utilize $timeout value of top_ids undefined @ point of assignment.

the easiest solution rename $scope.top_ids $scope.node_ids , rid of ng-init.

javascript angularjs

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -