3

I am getting $scope.persons from a back end. I list it using ng-repeat.

Why will the array not be modified in my case when $scope.openDeleteModal is called?

The function is called but nothing happens to $scope.persons. Why?

application.controller('listPersonController', function ($scope, personService) {
   personService.getPersons()
  .then(function(persons) {
      $scope.persons = persons;
  });

  $scope.openDeleteModal = function (personId, firstname, lastname, index) {
    console.log("click " + $scope.persons.length);
    $scope.persons.splice();
    console.log("click " + $scope.persons.length);
  };
});

Edit:

OK I forget about splice. I use this instead

 $scope.openDeleteModal = function (personId, firstname, lastname, index) {
    console.log("click " + $scope.persons.length);
    $scope.persons = [];
    console.log("click " + $scope.persons.length);
  };

The first one logs 3. The second 0. But I do not see any changes on the scope(ng-repeat).

The HTML:

<tbody>
   <tr ng-repeat="p in persons">
      <td>{{p.firstname}}</td>
      <td>{{p.lastname}}</td>
      <td><a href="#/register_person/{{p.id}}" class="tiny button radius" >Edit</a></td>
      <td>
        <a href="#" 
           ng-click="openDeleteModal(p.id, p.firstname, p.lastname, $index)"
           class="tiny button alert radius">Delete</a>
      </td>
   </tr>  
</tbody>

The service:

personService.getPersons = function($scope){
   var promise = $http.get("rest/person").then(function (response) {
        return response.data;
   });
   return promise;
};

This is what {{persons}} just under the controller gives

[{"id":"5331c6f33004e1c8a26492a8","firstname":"Karl","lastname":"Svensson"},{"id":"5331dfdb3004e1c8a26492ad","firstname":"Jenny","lastname":"Bertilsson"},{"id":"5331ee4e3004e1c8a26492ae","firstname":"Bosse","lastname":"Gustavsson"}]

This does not change either when I modify the scope

Edit Trying to simplify stuff even more.

application.controller('listPersonController', function ($scope, personService) {

  var personsList =  [{"id":"5331dfdb3004e1c8a26492ad","firstname":"Johnny","lastname":"Bravo"}];

  $scope.persons = personsList;

  $scope.openDeleteModal = function (personId, firstname, lastname, index) {
    console.log("click " + $scope.persons.length);
    $scope.persons.length = 0;
    personsList.length = 0;
    console.log("click " + $scope.persons.length);
  };
});

Edit: The html is super simple now

<div class="row" ng-controller="listPersonController" >
    <p ng-repeat="p in persons">{{p.firstname}} <a href="#" class="tiny button alert radius" ng-click="openDeleteModal(p.id, p.firstname, p.lastname, $index)" >Delete</a></p>
</div>

Even more simple. The value of var is 3 on my page and it does not get updated on click.

application.controller('listPersonController', function ($scope, personService) {

  $scope.var = "1";

  $scope.openDeleteModal = function () {
    $scope.var = "2";
  };

   $scope.var = "3";
});

Edit: Code works as expected when moved out from ng-view. Why is this happening?

25
  • 1
    Doesn't splice take 2 arguments? .splice(index, 1) Commented Mar 25, 2014 at 20:58
  • Well..that certainly is interesting - the view should most definitely update. Just for kicks tho - throw a $scope.apply() at the end of the method Commented Mar 25, 2014 at 21:05
  • Error: [$rootScope:inprog] $apply already in progress Commented Mar 25, 2014 at 21:09
  • Hmm...does the view ever update? After a few seconds or another Angular function? Commented Mar 25, 2014 at 21:13
  • 1
    Have you tried removing the href="#" from the link? That might be making the page navigate to itself and causing the controller to be re-instantiated and therfore getting a new version Commented Mar 26, 2014 at 9:46

2 Answers 2

3

Remove the href="#" from the link, it's probably causing the page to navigate to itself and causing the controller to be re-instantiated.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 U nailed it. The fact that, a controller getting refresh can alter the data structure is something we can miss out.
Wow, this was answered a long time ago. I'm glad it helped.
true, but I was new on angular was hunting for the unattended behavior. I had a button with <a href="#" ... then now its <a href="javascript:void(0)"..
1

The splice method is not destructive when not passed any arguments.

What that means is that while it will return the spliced (empty) array, it will not overwrite the original array.

What that means for you is that if you want to call splice to clear out the whole array, you should call:

$scope.persons = $scope.persons.splice()

Though (without referencing the javascript source) it is my belief that

$scope.persons = []

will probably be faster.

Keep in mind that this will trigger a full digest cycle, triggering every $watch and bound attribute you've defined to be hit.

1 Comment

as demoed here plnkr.co/edit/et9dxXR3cPUuthp7PxBN?p=preview your approach should work. Any other controllers/HTML you could post?

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.