0
我有不同的复选框,它们也有单独的端点。我想根据复选框选择相应的端点,并将所有结果返回到一个数组中以进一步过滤。到目前为止,我在网上找到的资源要求我使用$ q.all链接所有请求,但似乎无法根据所选的复选框来实现此目的。根据选中的复选框返回结果数组
这是我到目前为止。我需要帮助,请。
模板
<div class="col-sm-4" ng-repeat="item in checkBoxes">
<input type="checkbox" ng-model="item.selected">
<span>{{item.name}}</span>
</div>
<button ng-click="getResult()">Get Result</button>
控制器
$scope.checkBoxes = [
{
id: 1,
name: "option1",
selected: false
},
{
id: 2,
name: "option2",
selected: false
},
{
id: 3,
name: "option3",
selected: false
}
];
// Checking which option is checked
$scope.optionChecked = function(choice) {
$scope.details = [];
angular.forEach(choice, function(value, key) {
if (choice[key].selected) {
$scope.details.push(choice[key].name);
}
});
};
function isInArray(name,details) {
for (var i = 0; i < details.length; i++) {
if (details[i].toLowerCase() === name.toLowerCase()){
return true;
}
}
return false;
}
function loadPage() {
if (isInArray("option1",$scope.details)){
Servicename.endpoint1()
.success(function(response) {
console.log(response);
});
})
.error(function() {
console.error(arguments);
$scope.failed = true;
})
}
if (isInArray("option2",$scope.details)){
Servicename.endpoint2()
.success(function(response) {
console.log(response);
});
})
.error(function() {
console.error(arguments);
$scope.failed = true;
})
}
}
这是我想达到的效果。 finalResult从loadPage函数中获取。
$scope.getResult = function() {
$scope.optionChecked($scope.checkBoxes);
if($scope.details.length > 0 && $scope.details[0] !== null){
loadPage().then(function(finalResult) {
console.log("This should return the final array based on checked
boxes")
});
}
它工作。非常感谢@nicooga,我很感激。 – Hopez