bumchickbum97
bumchickbum97

Reputation: 121

http GET request based on true or false condition in angularjs

So basically I want to call all the posts when four conditions satisfy.

Initially,

a=false,
b=false,
c=false,
d=false

When the user selects these options these turn to true

For eg: When user selects a then a=true

Based on this condition I want to make a http.get request. Something like this

`listAllPosts(a,b,c,d){

GET /posts?a=true }

I need to make a GET request based on the condtions everytime one or more than one variable becomes true.

Sorry for bad explaination.

Upvotes: 0

Views: 668

Answers (2)

Hamza Baig
Hamza Baig

Reputation: 676

I believe a, b, c and d are associated with checkboxes. Then you can do the following:

html:

<div>
    <input type="checkbox" ng-change="sendRequest()" ng-model="a" />
    <input type="checkbox" ng-change="sendRequest()" ng-model="b" />
    <input type="checkbox" ng-change="sendRequest()" ng-model="c" />
    <input type="checkbox" ng-change="sendRequest()" ng-model="d" />
</div>

Controller:

$scope.a = false;
$scope.b = false;
$scope.c = false;
$scope.d = false;

$scope.sendRequest = function () { 
    if ($scope.a || $scope.b || $scope.c || $scope.d ) { 
        // Get request here
    }
}

Let me know if issues.

Upvotes: 1

Chris Noring
Chris Noring

Reputation: 471

So essentially you want a watch expression looking at all four variables. And when that combined expression is 1 ( truthy ) you want to do your ajax call

  $scope.a = false;
  $scope.b = false;
  $scope.c = false;
  $scope.d = false;
  $scope.$watch(function(){
    return $scope.a + $scope.b + $scope.c + $scope.d;
  }, function(newVal, oldVal) {
    if( newVal === 1 ) {
        console.log('do ajax call here');
    } 
  })

Upvotes: 0

Related Questions