使用ng-resource捕获自定义方法PUT成功

时间:2014-08-26 10:46:55

标签: angularjs put ngresource

我创建了一个方法类型为put的自定义方法更新。

我想抓住成功并在那里发动一些事件。

有没有人有公平的想法怎么做?

SERVICE

(function () {
'use strict';
 // this function is strict...

angular
    .module('zdma.organization.services', [])
   .factory('Organization', function($resource, Config) {

        var Organization = $resource(
                Config.url.organization + '/:id',
            {
                id: '@id' 
            },
            {
                'update': {
                    method: 'PUT'
                }
            }
        );

        // class methods
        Organization.getEmptyOrganizationTemplate = function () {
            return {
                'id': '',
                'name': '',
                'declarationCode': '',
                'website': ''
            };
        };
        return Organization;
    });
}());

在控制器中

angular
    .module('zdma.organization.controllers', [])
.controller('OrganizationCreateCtrl', function($scope, $location, Organization) {
    function init() {
        $scope.organization = new Organization(Organization.getEmptyOrganizationTemplate());
    }

    $scope.save = function() {   
        $scope.organization.$save();
    };
init();
});

在这种情况下,你能帮我解决这个问题吗?

请假设我的模型在视图的基础上更新

1 个答案:

答案 0 :(得分:0)

<强>控制器

angular.module('zdma.organization.controllers', [])
.controller('OrganizationCreateCtrl', function($scope, $location, Organization) {
    function init() {
        $scope.organization = new Organization(Organization.getEmptyOrganizationTemplate());
    }
    $scope.save = function() {   
        $scope.organization.$update(
           /* this function will be fired if you have a success */ 
           function(data){
               /* Put your events here */
           },
           /* this funciton will be fired if you have a failure */
           function(error){}
        );
    };
    init();
});