【问题标题】:Catch custom method PUT success with ng-resource使用 ng-resource 捕获自定义方法 PUT 成功
【发布时间】:2014-08-26 10:46:54
【问题描述】:

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

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

有人知道怎么做吗?

服务

(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();
});

你能帮我在这种情况下怎么做吗?

请假设我的模型基于视图更新

【问题讨论】:

    标签: angularjs put ngresource


    【解决方案1】:

    控制器

    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();
    });
    

    【讨论】:

    • 更新了我的问题,请看一下
    • @AdityaSethi 希望对您有所帮助。
    • 我正在使用适当的注射伙伴。我在控制器中注入了组织服务。为什么我不能使用 $scope.organization = new Organization($scope.editModel);然后通过回调应用更新?我实际上无法获得更新方法:( $scope.organization.$update(); 工作正常,但只能刷新。这就是为什么我只想在我无法找到的成功时重定向我的页面:(
    • 替换:$scope.organization.$update(); with : $scope.organization.update({},{},function(data){ /* 把你的代码放到她 / }, function(error){/ / 把你的代码放到她 */ );
    • @AdityaSethi 喜欢这样。
    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多