0

我创建了一个资源对象:

factory('TextResource', 
    function($resource) {
        return $resource(adminBaseUrl+'/texts/:type', {}, {
            create: {method: 'POST', params: {type:'create'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
            update: {method: 'POST', params: {type:'update'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
            query: {method: 'GET', params: {type: 'list'}},
            remove: {method: 'POST', params: {type: 'remove'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
            getText: {method: 'GET', params: {type: 'get', id:'@id'}}
        });
    }
)

我的控制器是:

controller('EditText', ['$scope', '$location', '$routeParams', 'TextResource', 'HttpStatusMessage',
    function($scope, $location, $routeParams, TextResource, HttpStatusMessage) {
        $scope.alerts = [];
        $scope.languages = [];

        TextResource.getText(
            {id: $routeParams.id},
            function(data) {
                $scope.languages = data.result;
            },
            function(error) {
                var httpError = new HttpStatusMessage(error.status);
                $scope.alerts.push({type:'error', msg:httpError.msg});
            });

        $scope.closeAlert = function(index) {
            $scope.alerts.splice(index, 1);
        }

        $scope.submit = function() {
            TextResource.update(
                $scope.languages,
                function(data) {
                    if( data.type == 'success' ) {
                        $location.path('texts');
                    } else {
                        $scope.alerts.push({type:data.type, msg:data.message});
                    }
                },
                function(error) {
                    var httpError = new HttpStatusMessage(error.status);
                    $scope.alerts.push({type:'error', msg:httpError.msg});
                });
        }

        $scope.cancel = function() {
            $location.path('texts');
        }
    }
])

我从 TextResource.getText 请求得到的响应是:

{"result":[{"id":"3","value":"This is my first text<br>","key":"my_first_text","language_id":"1","name":"English"},{"id":"3","value":"Ceci est mon premier texte","key":"my_first_text","language_id":"3","name":"French"}],"num_rows":2}

现在,当我单击提交时,它会显示错误:

Error: a.push is not a function

响应对象包含 2 个键结果,num_rows 结果是一个数组。我不在资源对象中使用 isArray 参数的原因是如果服务器中发生任何错误,例如会话超时、不允许访问等。服务器返回的对象包含错误消息。

4

1 回答 1

1

问题通过修改更新功能解决,如:

$scope.submit = function() {
            TextResource.update(
                {'language':$scope.languages},
                function(data) {
                    if( data.type == 'success' ) {
                        $location.path('texts');
                    } else {
                        $scope.alerts.push({type:data.type, msg:data.message});
                    }
                },
                function(error) {
                    var httpError = new HttpStatusMessage(error.status);
                    $scope.alerts.push({type:'error', msg:httpError.msg});
                });
        }

我直接在更新中发布了一个引发错误的数组。所以封装在另一个密钥中解决了这个问题。

于 2014-01-20T07:26:45.890 回答