我想返回包含對象數組的作用域變量的值長度。那麼,我的想法是減少html視圖並將變量存儲在我的控制器中。問題是,當我稱之爲我得到的總長度爲0在AngularJS中返回作用域變量的長度
下面的代碼被認爲是一個視圖控制檯值:
<div class="panel panel-default">
<div class="panel-body bg-form">
<div class="col-sm-4">
<strong>total:</strong>
</div>
<div class="col-sm-3">
<span class="badge"> {{ nameslist.length }}</span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body bg-form">
<div class="col-sm-4">
<strong>filtered:</strong>
</div>
<div class="col-sm-3">
<span class="badge"> {{ filteredNames.length }}</span>
</div>
</div>
</div>
...
<tr ng-repeat="names in filteredNames = (nameslist | orderBy:sortType:sortReverse)" class="show-cursor">
<td>{{ names.fname }}</td>
<td>{{ names.lname }}</td>
</tr>
這些應該是新的代碼:
<div class="panel panel-default" ng-repeat="sResult in searchResult">
<div class="panel-body bg-form">
<div class="col-sm-4">
<strong>{{ sResult.title }}:</strong>
</div>
<div class="col-sm-3">
<span class="badge"> {{ sResult.content }}</span>
</div>
</div>
</div>
Ctrl鍵:
$scope.nameslist = CrudService.getAllNames();
$scope.searchResult = [
{
title: 'total',
content: $scope.nameslist.length
},
{
title: 'filtered',
content: $scope.filteredNames.length
}
];
服務:
myApp.factory('CrudService', ['ResService',
function (ResService) {
return {
getAllNames: function() {
return ResService.names.query();
},
...
}]);
myApp.factory('ResService', ['$resource', 'baseUrl',
function ($resource, baseUrl) {
return {
names: $resource(baseUrl + '/api/names/:Id/:fname', {
Id: '@Id',
fname: '@fname'
}, {
'update': {
method: 'PUT'
}
}),
...
}]);
如何僅返回數組的數量和過濾的數組的數量?
也許你只是想在控制檯上評估$ scope.searchResult.length時,你是不是在模塊方面? $ scope不是全局變量 – Black0ut
您的控制器似乎將您的CrudService當作同步對待。 –
@ BlackOut爲什麼我應該返回searchResult的長度..我需要名稱列表和filteredNames的長度。 – yuro