Search for element in an array using array.foreach

Asked

Viewed 1,275 times

0

I have a grid where on each line there is a button to delete the corresponding data. This grid is powered by a json but there is no id. I wonder how I could delete the selected object, being that the grid is being fed by an ng-repeat and to delete a data I have to take the click, remove the item from the array and give a post with all the data to the server back.

Follow what I’m trying to do (and it’s not working)

ng.remover = function(liberados){
    var ind;
    allow.url.forEach(function (lib, i){
        if(liberados == lib){
            ind = i;
        }
    });
    allow.url.splice(1,ind);
    var allowCopied = angular.copy(allow);      
    allowCopied.name = allowCopied.url;

    ng.tudo.allowedSitesList.push( allowCopied );
    $http({method: "POST", url: "http://localhost:0000/in", data: ng.tudo})
        .then(function (dados) {});     
}

1 answer

1


I don’t understand what you wanted to do in your role, but in case you want to compare if it contains an item in an angular foreach, try to do so:

$scope.allow = [{"url":"google.com"}, {"url":"uol.com"}, {"url":"globo.com"}, {"url":"msn.com"}];

$scope.denied = [];

$scope.remove = function(urlLiberada){
    //chama o foreach do angular
    angular.forEach($scope.allow, function (item,index){
        //verifica se contem
        if(item.url.indexOf(urlLiberada) != -1){
            //adiciona o item a lista denied
            $scope.denied.push(item);   
            //remove da lista allow
            $scope.allow.splice(index);                  
        }
    });
}

//testando a funçao removendo a url msn.com
$scope.remove("msn.com")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.