I'm trying to get the response of a $http call from ng-repeat inputs. And then determining whether to show the individual ng-repeat's given each response. Currently the actual $http api isn't available, so I've just hardcoded the response.
It works just fine without using promises. But I am unable to even run it on jsfiddle when using the promise method. How can I get the promise working?
EDIT: I've reworked my question thanks to Benjamin Gruenbaum I'm not actually using $http but using pouchDB (local DB so no need to worry about DOS). Just using $http as an example since it returns a promise as well. What I really need to work out is how to get the promises to update again when adding new names. e.g If "joe" is added then he should be on 'billsNotPaid'
http://jsfiddle.net/TheSlamminSalmon/cs0gxxxd/6/
View:
<div ng-app>
<div ng-controller="MainController">
<h1>Hello Plunker!</h1>
<form ng-submit="AddNewGuy()">
<input type="text" ng-model="newGuy" placeholder="Insert New Guy"></input>
<input type="submit"></input>
</form>
<strong>Everyone</strong>
<div ng-repeat="name in names">
{{name}}
</div>
<strong>These guys haven't paid their bills (Non Promise)</strong>
<div ng-repeat="name in names">
<div ng-show="NotPaidBillsNonPromise(name)">
{{name}}
</div>
</div>
<strong>These guys haven't paid their bills (Using http Promise)</strong>
<div ng-repeat="name in billsNotPaid">
{{name}}
</div>
</div>
</div>
AngularJS Controller:
function MainController($scope, $http) {
$scope.names = [
"James",
"Tim",
"Alex",
"Sam",
"Kim"
];
$scope.billsNotPaid = []; // start as empty
$scope.NotPaidBillsNonPromise = function (name) {
if (name == "Tim" || name == "Sam" || name == "Joe") return true;
};
$scope.NotPaidBills = function (name) {
return $http.get("http://echo.jsontest.com/name/" + name)
.then(function (r) {
return (r.data.name === "Tim" || r.data.name === "Sam" || r.data.name === "Joe")
});
};
$scope.newGuy;
$scope.AddNewGuy = function () {
$scope.names.push($scope.newGuy);
};
// start the check for each name
$scope.names.forEach(function(name){
return $scope.NotPaidBills(name).then(function(notPaid){
console.log(name, notPaid);
if(notPaid) $scope.billsNotPaid.push(name);
});
});
}