0

I'm trying to build my first hybrid-app by using iOnic. iOnic makes use of AngularJS. But both are totally new for me :)

So far I'm pretty happy with the results I made because I can find a lot of stuff if I'm stuck, but now I'm lost.

I'm trying to delete a favorite object I have saved in a local JSON file by clicking the remove button that I have placed behind every favorite item.

HTML template where the favorite items are listed by ng-repeat:

<ion-view view-title="My Favorites">
<ion-content>
    <ion-list>
      <ion-item ng-repeat="favo in favos" class="row">
        <i class="col icon ion-{{favo.gender}}"></i>
        <a href="#/app/myFavos/{{favo.id}}" class="button-clear col col-80">
            {{favo.name}}
        </a>
        <a class="button button-icon icon ion-trash-b col" ng-click="removeFavo({{favo}})">
        </a>
      </ion-item>
    </ion-list>
</ion-content>

Controller.js file:

angular.module('starter.controllers', [])

.controller('FavoslistCtrl', function($scope,  myFavos) {

    myFavos.getFavos(function (data) {
        console.log(data);
        $scope.favos = data;
    });

    $scope.removeFavo = function(data) {

        $scope.favos = myFavos.deleteFavo(data);

    };

});

Service.js file:

angular.module('starter.services', [])

.factory('myFavos', function($ionicLoading, $http) {


  // Might use a resource here that returns a JSON array
  return {

    getFavos: function(callback) {
      $ionicLoading.show();
      $http.get('data/favos.json').success(function (data) {
        console.log("http GET success my favorites");

        console.log("return myFavos", data);
        $ionicLoading.hide();
        return callback(data);

      }).error(function (err) {
        $scope.error = 'Could not load favorites';
      });
    },

    deleteFavo: function(favo) {
$ionicLoading.show();
      $http.delete('data/favos/' + favo + '.json').success(function (data) {
$ionicLoading.hide();
        return callback(data);
      });
    }
  }

});

favos.json:

[
    {
        "id":1,"name":"Adam","description": "Lorem Ipsum", "gender": "male"
    },
    {
        "id":2,"name":"Eva","description": "Lorem Ipsum", "gender": "female"
    },
    {
        "id":3,"name":"alex","description": "Lorem Ipsum", "gender": "male"
    },
    {
        "id":4,"name":"adam","description": "Lorem Ipsum", "gender": "male"
    },
    {
        "id":5,"name":"max","description": "Lorem Ipsum", "gender": "male"
    }
]

I have been trying it with the $http.delete function in the factory I made called 'myFavos' but I can't figure out how to delete the right object, with the right id.

If someone could help that would be awesome!

UPDATE 08-12-2015 10:37am

I'm a step closer now, thanks to the reply of Gajotres on my post on the iOnic forum The only error I get now in the console is:

DELETE http://localhost:8100/data/favos/[object%20object].json 404(Not Found)

I updated the code to the current state.

2
  • You only want to delete it locally or you also want to delete it on the server ? Commented Jul 31, 2015 at 14:09
  • @GuillaumeP only locally Commented Aug 11, 2015 at 9:03

1 Answer 1

0
$scope.removeFavo = function(data) {
    myFavos.deleteFavo(favo); // <=== shouldn't this be data?
}

Are you using the correct variable??

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

2 Comments

Your suggestion was good, there was a problem with the variable and the properties I used. I changed the code and I'm one step closer, now I get a 404 from the http.delete function.
my guess is that you should be passing in an object id, not the whole object

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.