Angularjs 当服务提供未过滤的数组但不提供';在使用下划线时,不使用下划线
我有一个角度服务,它包含两个函数来返回一个对象数组。第一个函数返回整个数组,第二个函数返回基于ID的过滤数组。当我使用第一个函数返回所有对象时,数据绑定会自动工作,只要我将新对象推送到数组中,我的前端就会更新。但是,当我使用使用下划线返回筛选列表的函数时,我的前端不会自动更新 我已经做了一些研究,我所看到的唯一类似的事情是异步请求和使用承诺,但我不确定这在这种情况下是否合适,因为我目前只在服务对象中使用 服务Angularjs 当服务提供未过滤的数组但不提供';在使用下划线时,不使用下划线,angularjs,underscore.js,angular-services,Angularjs,Underscore.js,Angular Services,我有一个角度服务,它包含两个函数来返回一个对象数组。第一个函数返回整个数组,第二个函数返回基于ID的过滤数组。当我使用第一个函数返回所有对象时,数据绑定会自动工作,只要我将新对象推送到数组中,我的前端就会更新。但是,当我使用使用下划线返回筛选列表的函数时,我的前端不会自动更新 我已经做了一些研究,我所看到的唯一类似的事情是异步请求和使用承诺,但我不确定这在这种情况下是否合适,因为我目前只在服务对象中使用 服务 angular.module('RankingsApp') .service('res
angular.module('RankingsApp')
.service('results', function() {
var uid = 2;
var results = [
{
"id":1,
"fencer":1,
"competition":1,
"placing":1,
"points":50
}
];
this.getResults = function()
{
return results;
}
this.getResultsForCompetition = function (_id)
{
var resultsForCompetition = _.filter(results, function(x){ return x.competition == _id});
return resultsForCompetition;
};
this.insertResult = function (result) {
result.id = uid++;
results.push(result);
};
});
控制器
angular.module('RankingsApp')
.controller('CompetitionCtrl', function ($scope, competitions,fencers,results, $routeParams) {
$scope.getResults = function()
{
return results.getResultsForCompetition($routeParams.competitionID);
}
$scope.competition = competitions.getCompetition($routeParams.competitionID);
$scope.fencers = fencers.getFencers();
$scope.compResults = results.getResultsForCompetition($routeParams.competitionID);
function getNextPlacing()
{
return $scope.compResults.length + 1;
}
$scope.getFencerFromResult = function(result)
{
return fencers.getFencer(result.fencer);
}
$scope.getCompFromResult = function(result)
{
return competitions.getCompetition(result.competition);
}
$scope.addNewResult = function(fencer)
{
var result = { "fencer": fencer.id, "competition":$routeParams.competitionID, "placing": getNextPlacing(), "points":50 };
results.insertResult(result);
$scope.selectedFencer = null;
}
});
查看
<table style="width: 100%">
<thead>
<tr>
<th>Placeing</th>
<th>Fencer</th>
<th>Comp</th>
<th>Points</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='result in compResults'>
<td>{{result.placing}}</td>
<td>{{getFencerFromResult(result).firstname}} {{getFencerFromResult(result).lastname}}</td>
<td>{{getCompFromResult(result).shortName}}</td>
<td>{{result.points}}</td>
<td><a>Edit</a></td>
</tr>
</tbody>
</table>
<tr ng-repeat='result in getResultsForCompetition(competitionID)'>
放置
击剑运动员
公司
要点
编辑
{{result.placeing}
{{getFencerFromResult(结果).firstname}{{getFencerFromResult(结果).lastname}}
{{getCompFromResult(result).shortName}}
{{result.points}
编辑
这是因为您的方法(使用..filter()
)返回的是另一个数组,而不是前端视图绑定到的数组(在数组
或对象
的情况下,绑定是通过引用完成的)
要解决此问题,您可以在视图中放置过滤逻辑并使用ng repeat
如果不是选项,则应使用pop()
/push()
方法直接修改服务中的results
变量
更新:
<tr ng-repeat='result in compResults'>
使用@Mironor发布的建议找到,我能够想出以下解决问题的解决方案。通过将ng repeat更改为直接调用函数,当我向服务推送新值时,列表会自动更新
查看
<table style="width: 100%">
<thead>
<tr>
<th>Placeing</th>
<th>Fencer</th>
<th>Comp</th>
<th>Points</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='result in compResults'>
<td>{{result.placing}}</td>
<td>{{getFencerFromResult(result).firstname}} {{getFencerFromResult(result).lastname}}</td>
<td>{{getCompFromResult(result).shortName}}</td>
<td>{{result.points}}</td>
<td><a>Edit</a></td>
</tr>
</tbody>
</table>
<tr ng-repeat='result in getResultsForCompetition(competitionID)'>
我已经用视图更新了我的帖子,当你说视图中的过滤器时,你能解释一下你的意思吗?您的意思是使用ng repeat向服务打电话吗?返回所有结果是否浪费时间?例如,如果阵列中有1000+或2000+个元素呢?当然,如果阵列中有10000个元素需要过滤,则需要进行优化。但这将是另一个问题。如果您的服务需要这样的优化,那么angularjs绑定是您首先需要摆脱的东西,并开始直接呈现(使用分页)。并且您不会返回所有结果,而是返回指向数组的指针,并非整个结构需要记住的是,在摘要周期中,angularJs将重新评估getResultsForCompetition(competitionID),以呈现新的或删除删除的模型。我怀疑这比ng repeat中包含的过滤器快。