0

I am working on a note-taking app.

I want my app to delete all checked radios on clicking the 'Remove' link. An insight into the code:

HTML:

<p>[ <a href='#/home'>Cancel</a> | <a href='#/home/edit' ng-click='remove()'>Remove</a> ]</p>
<table border='0'>
  <tbody>
    <tr ng-repeat='note in notes'>
      <td>
        <input type='radio' ng-model='note.rmv'></input>
      </td>
      <td>{{note.text}}</td>
    </tr>
  </tbody>
</table>

Controller:

app.controller('editCtrl', ['$scope', 'notes', function($scope, notes) {
  $scope.notes = notes.notes;
  $scope.remove = function() {
    for (var i = 0; i < $scope.notes.length; ++i) {
      if ($scope.notes[i].rmv) {
        delete $scope.notes[i];
        notes.getLost($scope.notes[i]._id);
      }
    }
  };
}]);

Factory:

app.factory('notes', ['$http', function($http) {
  var t = {
    notes: []
  };
  t.getLost = function(id) {
    return $http.delete('/home/edit').success(function(data) {
      return t.getAll();
    });
  };
  return t;
};

What might be doing wrong here?

8
  • 2
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please edit these details in or we may not be able to help. Commented Aug 7, 2016 at 3:50
  • Possible duplicate of Angular element remove does not work ? Commented Aug 7, 2016 at 3:52
  • How does $scope.notes suddenly change to $notes.notes ? Meaning that you first loop $scope.notes then in that loop you check $notes.notes[i].. Commented Aug 7, 2016 at 4:00
  • I don't think <input> has an end </input> tag either Commented Aug 7, 2016 at 4:07
  • 1
    your getLost is really lost.. :) read the angular docs.. you should rewrite whole thing.. Commented Aug 7, 2016 at 4:35

2 Answers 2

2

Well, there are a lot of mistakes in your code, I think you should refactor your code. Also there's no necessity of delete the item in Javascript, you can delegate it all to your back-end since you already have the function to getAll objects.

See the code below to take it as example:

(function() {
  angular
    .module('app', [])
    .controller('editCtrl', editCtrl)
    .factory('notes', notes);

  editCtrl.$inject = ['$scope', 'notes'];

  function editCtrl($scope, notes) {
    getAll(); // <- initialization of notes
    $scope.remove = remove;

    function getSuccess(response) {
      console.log('success');
    }

    function getError(response) {
      console.log('error');
    }

    function remove() {
      for (var i = 0; i < $scope.notes.length; ++i) {
        if ($scope.notes[i].rmv) {
          notes.getLost($scope.notes[i]._id)
            .then(getSuccess)
            .catch(getError);
        }
      }
      fetchData();
    }

    function fetchData() {
      notes.getAll()
        .then(function(response) {
          $scope.notes = response.data;
        })
        .catch(function(response) {
          console.log('error');
        });
    }
  }

  notes.$inject = ['$http'];

  function notes($http) {
    var factory = {
      getAll: getAll,
      getLost: getLost
    };

    return factory;

    function getAll() {
      return $http.get('url_to_fetch');
    }

    function getLost(id) {
      // It should only return the promise, not more than that
      return $http.delete('/home/edit/' + id); // <- is this URL correct?
    }
  }
})();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, although it's part of a MEAN app and wouldn't make complete sense to you
Well, unfortunately I have to go ahead and say: even if anyone of us know your whole application, it's wrong in many parts.. by the way, modyfing the part of deletion make it works? If so, can you check it as answer?
0

You have a typo:

if($notes.notes[i].rmv) {

Should be:

if($scope.notes[i].rmv) {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.