【发布时间】:2017-05-28 22:49:21
【问题描述】:
我已经编辑了我的代码并将过滤器移到了我的控制器中
<div class="tab t-1"
ng-class="{'active' : legals.activeTab === 1}"
ng-show="legals.activeTab === 1">
<br>
<label>
Sort by
<select ng-model="sortProp" class="input-medium">
<option value="price">price</option>
<option value="minutes">minutes from the nearest station</option>
</select>
</label>
<select ng-model="locationFilter" ng-options="l as l for l in found">
<option value="">all</option>
</select>
<div class="form-group">
<input type="checkbox" ng-click="findRecent()" ng-checked=""/> from the last 7 days
</div>
<div ng-repeat="value in all | filter: recentFilter | orderBy:sortProp">
<ul>
<li><strong>{{value.id}}</strong></li>
<li><strong>{{value.address}}</strong></li>
<li>city: {{value.city}}</li>
<li>postcode: {{value.postcode}}</li>
<li>price: {{value.price}}</li>
<li>num_of_beds: {{value.num_of_beds}}</li>
<li>{{value.type}}</li>
<li>{{value.minutes}}</li>
<li>{{value.added}}</li>
</ul>
</div>
</div>
我将过滤器代码移动到我的控制器并尝试在 html 中使用它
var isContractEndDate = function(endDate) {
var DAYS_UNTIL_END = 8;
endDate = new Date(endDate);
var lastDate = new Date(new Date().setDate(new Date().getDate() - DAYS_UNTIL_END));
if (endDate > lastDate) {
return true;
}
return false;
};
$scope.foundRecent = [];
$scope.lookup = {};
$scope.findRecent = function() {
angular.forEach($scope.all, function(value, key) {
$scope.lookup = value;
if (isContractEndDate($scope.lookup.added)) {
$scope.foundRecent.push($scope.lookup);
}
});
return $scope.foundRecent;
};
$scope.recentFilter = function(searchResults) {
if ($scope.findRecent.length > 0) {
return searchResults;
}
};
但是它也没有过滤,我认为recentFilter函数存在一些问题
【问题讨论】:
-
将过滤器移动到控制器中的一个函数,并使用
ng-click使用单击按钮时的数据调用它 -
我这样做了,但我认为也许有更好的解决方案,我可以实现 ng-model
标签: angularjs filtering angular-ngmodel