我的角度应用中有一个用户列表,如果用户if
将其推入age > 45
阵列,我想要做的就是过滤validForScheme
条件的用户它推动了阵列中的所有用户。我无法弄清楚出了什么问题。
另一件事是ng-repeat
显示一个额外的空白项目。
注意:我使用$scope.allUsers
方法从数据库中获取http
。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = [];
$scope.filterUsers = function(arg) {
if (arg.age > 45)
console.log(arg.name, 'is valid for Scheme');
$scope.validForScheme.push(arg);
}
$scope.allUsers.forEach(function(item, index){
$scope.filterUsers(item);
})
console.log($scope.validForScheme);
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<ol>
<li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}<li>
</ol>
</div>
&#13;
答案 0 :(得分:3)
直接使用Array.filter
代替。像这样:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = $scope.allUsers.filter(function(item, index){
return item.age > 45
})
console.log($scope.validForScheme);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<ol>
<li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}</li>
</ol>
</div>
答案 1 :(得分:2)
您需要在if
{}
中附上多个语句。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = [];
$scope.filterUsers = function(arg) {
if (arg.age > 45){
//console.log(arg.name, 'is valid for Scheme');
$scope.validForScheme.push(arg);
}
}
$scope.allUsers.forEach(function(item, index){
//console.log(item)
$scope.filterUsers(item);
})
//console.log($scope.validForScheme);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<ol>
<li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}</li>
</ol>
</div>
&#13;
答案 2 :(得分:1)
您需要将if
语句的内容括在大括号{}
中。
如果没有括在大括号中,if
只考虑一个(立即)行/语句作为其范围。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.allUsers = [{
"age": 54,
"name": "Corrine Benson"
}, {
"age": 38,
"name": "Thompson Newton"
}, {
"age": 62,
"name": "Molly Good"
}, {
"age": 66,
"name": "Kristine Mcbride"
}, {
"age": 45,
"name": "Kathy Estrada"
}, {
"age": 22,
"name": "Krystal Davis"
}, {
"age": 53,
"name": "Butler Greene"
}, {
"age": 34,
"name": "Hogan Joseph"
}];
$scope.validForScheme = [];
$scope.filterUsers = function(arg) {
if (arg.age > 45) {
console.log(arg.name, 'is valid for Scheme');
$scope.validForScheme.push(arg);
}
}
$scope.allUsers.forEach(function(item, index) {
$scope.filterUsers(item);
})
console.log($scope.validForScheme);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<ol>
<li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}
</li>
</ol>
</div>
答案 3 :(得分:1)
如果陈述缺少括号
$scope.filterUsers = function (arg) {
if (arg.age > 45) {
console.log(arg.name, 'is valid for Scheme');
$scope.validForScheme.push(arg);
}
}
工作代码。
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<ol>
<li ng-repeat="users in validForScheme">{{users.name}}, {{users.age}}
<li>
</ol>
</div>
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.allUsers = [{
"age": 54,
"name": "Corrine Benson"
}, {
"age": 38,
"name": "Thompson Newton"
}, {
"age": 62,
"name": "Molly Good"
}, {
"age": 66,
"name": "Kristine Mcbride"
}, {
"age": 45,
"name": "Kathy Estrada"
}, {
"age": 22,
"name": "Krystal Davis"
}, {
"age": 53,
"name": "Butler Greene"
}, {
"age": 34,
"name": "Hogan Joseph"
}];
$scope.validForScheme = [];
$scope.filterUsers = function (arg) {
if (arg.age > 45) {
console.log(arg.name, 'is valid for Scheme');
$scope.validForScheme.push(arg);
}
}
$scope.allUsers.forEach(function (item, index) {
$scope.filterUsers(item);
})
console.log($scope.validForScheme);
});
</script>
</body>
</html>
&#13;
答案 4 :(得分:0)
validForScheme = allUsers.filter(user => user.age > 45)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
答案 5 :(得分:0)
为了更简单,更清晰,更可靠的实现,只使用数组过滤器:
$scope.allUsers = [{"age": 54,"name": "Corrine Benson"},{"age": 38,"name": "Thompson Newton"},{"age": 62,"name": "Molly Good"},{"age": 66,"name": "Kristine Mcbride"},{"age": 45,"name": "Kathy Estrada"},{"age": 22,"name": "Krystal Davis"},{"age": 53,"name": "Butler Greene"},{"age": 34,"name": "Hogan Joseph"}];
$scope.validForScheme = $scope.allUsers.filter(user => user.age > 45);
答案 6 :(得分:0)
你错过了条件之后的括号,这就是为什么立即将console.log视为条件语句而不是你的$scope.validForScheme.push(arg);