jeudi 26 novembre 2015

angular calling web api delete

Here's my service:

mystuffServices.factory("Country", function ($resource) {
    return $resource("/api/countries/:id", { id: '@Id' }, {
        update: {
            method: 'PUT'
        }
    });
});

routeprovider

mystuffApp.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.        
        when('/countries', {
            templateUrl: '../Views/Countries/index.html',
            controller: 'CountryListCtrl'
        }).
        when('/countries/create', {
            templateUrl: '../Views/Countries/create.html',
            controller: 'CountryCreateCtrl'
        }).
        when('/countries/:Id', {
            templateUrl: '../Views/Countries/detail.html',
            controller: 'CountryDetailCtrl'
        }).
        when('/countries/:Id/edit', {
            templateUrl: '../Views/Countries/edit.html',
            controller: 'CountryEditCtrl'
        }).
        otherwise({
            redirectTo: '/countries'
        });
  }]);

I can delete a country from CountryDetailCtrl in this way:

mystuffControllers.controller('CountryDetailCtrl', ['$scope', '$routeParams', 'Country','$uibModal','$location',
    function ($scope, $routeParams, Country, $uibModal, $location) {
        $scope.country = Country.get({ Id: $routeParams.Id });

        $scope.deleteCountry = function (country) {
            country.$delete(function () {
                $location.path('/countries');
            });
        };

        $scope.confirmDelete = function (size) {

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: '../Views/Modals/deleteCountryModal.html',
                controller: 'ModalInstanceCtrl',
                size: size
            });

            modalInstance.result.then(function () {
                $scope.deleteCountry($scope.country);
            });
        };
    }

]);

But when I'm trying to do the same from CountryListCtrl (when I see the list of countries, I don't want to enter country's details to delete it) I see "405 Method Not Allowed", "The requested resource does not support http method". I can see that it works if request url is http://localhost:59673/api/countries/id but it fails when it is http://localhost:59673/api/countries.

Here's my delete attempt in CountryListCtrl:

mystuffControllers.controller('CountryListCtrl', ['$scope', 'Country','$uibModal',
    function ($scope, Country, $uibModal) {

        $scope.countries = Country.query();    

        $scope.deleteCountry = function (country) {
            country.$delete(function () {
                $location.path('/countries');
            });
        };

        $scope.confirmDelete = function (size, id) {            

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: '../Views/Modals/deleteCountryModal.html',
                controller: 'ModalInstanceCtrl',
                size: size
            });

            modalInstance.result.then(function () {
                $scope.country = Country.get({ Id: id });                
                $scope.deleteCountry($scope.country);
            });
        };
}
]);

I believe I should change my factory code to be able to use delete in my 'custom' way but I don't know how. I'm terrible sorry, if I'm asking about some common, easy things. I just started learning angular and web api, trying to make an app editing codes which I found over internet and learn this way.

Thank you.




Aucun commentaire:

Enregistrer un commentaire