gpt4 book ai didi

javascript - AngularJS 更新 ng-include 和 ng-repeat 中的 $scope

转载 作者:行者123 更新时间:2023-11-28 04:47:41 25 4
gpt4 key购买 nike

<ul>
<li ng-repeat="folder in folderNames" ng-include="'test.html'">
</li>
</ul>
<script type="text/ng-template" id="test.html">
<ul> {{ folder.name }}
<li ng-repeat="folder in folder.children" ng-include="test.html"></li>
</ul>
</script>

<script>
$scope.folderNames = [{
name: 'a',
children: [{
name: 'b',
children: []
}]
}];
</script>

我想像树一样输出数据,但是当我使用ng-click事件函数更改$scope.folderNames值时。但实际上 View 并没有改变,为什么?我该怎么办?

最佳答案

尝试这样。

var app = angular.module("myApp",[]);
app.controller('ctrl',['$scope', function($scope){


$scope.showFolders = function(){
$scope.folderNames = [{
name: 'app',
children: [{
name: 'css',
children: []
},{
name: 'images',
children: []
}]
},{
name: 'folter1',
children: [{
name: 'sub-folder1',
children: []
},{
name: 'sub-folder2',
children: [{ name: 'sub-folder 2.1',children: [{
name: 'second-sub-of-sub',children: []},{
name: 'sub-of-sub',
children: []
}]
},{
name: 'sub-of-2.2',
children: []
}]
}]
}];
}

}]);
<html>
<head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="ctrl">
<button ng-click="showFolders()">View folders</button>
<ul>
<li ng-repeat="folder in folderNames">
{{ folder.name }}
<ul ng-include="'test.html'"></ul>
</li>
</ul>
</div>
<script type="text/ng-template" id="test.html">
<li ng-repeat="folder in folder.children">{{folder.name}}<ul ng-include="'test.html'"></ul>
</li>
</script>
</body>
</html>

关于javascript - AngularJS 更新 ng-include 和 ng-repeat 中的 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43209740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com