gpt4 book ai didi

javascript - AngularJS - 刷新 ng-repeat

转载 作者:IT王子 更新时间:2023-10-29 03:11:27 24 4
gpt4 key购买 nike

我正在使用 ng-repeat 来显示值的集合。我的过滤器选项根据对服务器的 ajax 调用而改变。接收到过滤器参数后如何刷新 ng-repeat?

js fiddle

模板

<div>
<div data-ng-controller="myCtrl">
<ul>
<li data-ng-repeat="item in values | filter:filterIds()">
<code>#{{item.id}}</code> Item
</li>
</ul>
</div>
</div>
<button ng-click="loadNewFilter()"> filter now</button>

Angular

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
$scope.values = [
{id: 1},
{id: 2},
{id: 3},
{id: 4},
{id: 5},
{id: 6}
];

$scope.filter = [1,2,3,4,5,6];

$scope.filterIds = function (ids) {
return function (item) {
var filter = $scope.filter;
return filter.indexOf(item.id) !== -1;
}
}

$scope.loadNewFilter = function (){
$scope.filter = [1,2,3];
}
});

最佳答案

您需要告诉 Angular 这些值发生了变化:

$scope.loadNewFilter = function (){
$scope.filter = [1,2,3];
$scope.$apply();
}

关于javascript - AngularJS - 刷新 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25843515/

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