gpt4 book ai didi

javascript - 以绑定(bind) ng-repeat 的一种方式重新呈现数据

转载 作者:行者123 更新时间:2023-11-27 23:46:08 25 4
gpt4 key购买 nike

我在 ng-repeat 中使用一种绑定(bind)方式。我仍然想在数组值更改时刷新 ng-repeat

请提出您的想法:

示例:

<html ng-app="todoApp">

<head>
<title>
TO DO List
</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/readable/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<script data-require="angular.js@*" data-semver="1.3.1" src="//code.angularjs.org/1.3.1/angular.js">
</script>
<script>
var todoApp = angular.module("todoApp", []).config(function($compileProvider, $httpProvider) {
$compileProvider.debugInfoEnabled(true);
});

todoApp.controller("ToDoCtrl", function($scope) {

$scope.data = [12, 23, 45, 67, 89];
$scope.add = function(dtata) {
$scope.data.push(dtata);
}
});
</script>

</head>

<body ng-controller="ToDoCtrl">

<!-- ...elements omitted for brevity... -->

<div class="panel">
<div class="input-group">
<input class="form-control" />
<input type="text" ng-model="numberData" />
<button type="submit" ng-click="add(numberData)">
add
</button>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>
Numbers
</th>
</tr>
<tr ng-repeat="de in ::data">
<th>
{{::de}}
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>

</html>

可以在此处找到代码 Plnkr

最佳答案

使用“::”语法用于一次性 绑定(bind),不应与单向 绑定(bind)混淆。 ng-repeat 本质上是一种方式。也就是说,添加dom元素并不会神奇地将数据项添加到模型中。一次性绑定(bind)的真正目的是通过明确声明用于渲染的数据不会改变(或者即使改变,我们也不会在意)来提高 Angular 摘要循环的性能。

话虽如此,您可以从 {{::de}} 中删除 :: 以消除检查每个元素是否发生变化的开销。如果 data 变得非常大,这将避免性能下降。

关于javascript - 以绑定(bind) ng-repeat 的一种方式重新呈现数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828705/

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