javascriptangularjsrequest-cancelling

angularjs checking data before request


I'm sending data to my php script. Data looks like enter image description here

Controller:

app.controller('searchCtrl', function($scope,$rootScope,$http,$q){
  $scope.search = function( values , type) {
    if($scope.abort) {
        $scope.abort.resolve();
    }
    $scope.abort = $q.defer();
    var data={};
    data.values=values;
    data.type=type;

    $http.post("search.php", data,{timeout:$scope.abort.promise}).then(function success (response) {

            $rootScope.search_result=response.data;
        },function error (response){
            console.log("Request cancelled");
        }
    );
  };
});

But I want to check if in at list one array item has property val!=""; Is there any way to check it besides cycle like

var flag=0;
for(var i=0; i<values.length; i++){
    if(values[i].val!="") flag=1;
}
if(flag)$http...

?


Solution

  • check find method

    var flag = data.values.find(function(element){
         return element.val != ''
    })
    
    if(flag)$http...