Если я изменил $ scope после $ timeout, мое представление не получилось должным образом. Я не понимаю, почему. Он работает, если я сначала не делаю $ timeout.Угловой взгляд не обновляется
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) {
// This 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]
// This 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>
Попробуйте назначить переменную $ scope переменной и использовать эту переменную в тайм-ауте или, возможно, предоставить скрипт – Fyre
Я попытался назначить переменную $ scope переменной, но это не устранило проблему. Я предоставил plunkr (см. Ссылку прямо над кодом). –
Никогда не используйте 'ng-init', если вам не нужно сохранять внешний индекс в вложенном' ng-repeat'. – Miraage