0

I have a controller which has the test array and I have added 2 objects to it. When I call delete function from html ng-click, the array is not updated in dom but I can see that the array being spliced in js code. I have tried $apply(), but that does not work as it says it is already applied.

app.controller('HomeCtrl', ['$rootScope', '$scope', function($rootScope, $scope) {

    var person1 = {firstName:"John", lastName:"Doe", age:46}; 
    var person2 = {firstName:"Joe", lastName:"Horn", age:66}; 
    $scope.test = []; 
    $scope.test.push(person1);
    $scope.test.push(person2);

    $scope.deletePost = function(value, post){       
            $scope.test.splice(0, 1);
    }
}]);

HTML:

<div class="row" ng-controller="HomeCtrl">
 <div ng-repeat="tes in test">{{tes.age}}<br></div>
</div>
 <a data-toggle="modal" ng-click="deletePost(value, post)" data-target="#deleteModal"  ng-show="post.user.email == user.email" style="font-size:15px;margin-left: 1rem;" class="card-link" href="javascript:void(0)">Delete</a>
8
  • How do you call deletePost method ? Commented Jun 13, 2016 at 15:26
  • @Vincismique added in post now. Commented Jun 13, 2016 at 15:29
  • is "initiateDelete" supposed to be "deletePost"? Commented Jun 13, 2016 at 15:30
  • your code seems to work Perfectly. Check this plunker based on your code. plnkr.co/edit/w1QZ4OkaEHTKg3TSSBcH?p=preview Commented Jun 13, 2016 at 15:30
  • @Deep The original code is very large to be posted here, i guess there is some other problem. Commented Jun 13, 2016 at 15:33

2 Answers 2

1

You need to rearrange your dom and you also should splice based on index.

Try:

DOM :

<div class="row" ng-controller="HomeCtrl">
 <div ng-repeat="tes in test track by $index">{{tes.age}}<br></div>
 <a data-toggle="modal" ng-click="deletePost($index, tes)" data-target="#deleteModal"  ng-show="post.user.email == user.email" style="font-size:15px;margin-left: 1rem;" class="card-link" href="javascript:void(0)">Delete</a>
</div>

Controller:

app.controller('HomeCtrl', ['$rootScope', '$scope', function($rootScope, $scope) {

    $scope.test = [{firstName:"John", lastName:"Doe", age:46}, {firstName:"Joe", lastName:"Horn", age:66}]; 

    $scope.deletePost = function(ind, post){       
            $scope.test.splice(ind, 1);
    }
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

Move <a> inside element which has your controller attached.

<div class="row" ng-controller="HomeCtrl">
 <div ng-repeat="tes in test">{{tes.age}}<br></div>
 <a data-toggle="modal" ng-click="deletePost(post, value)" data-target="#deleteModal"  ng-show="post.user.email == user.email" style="font-size:15px;margin-left: 1rem;" class="card-link" href="javascript:void(0)">Delete</a>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.