0

I'm trying to set up a form which add a new object to db but first I want to check something and set up the server side for new record, but I'm frozen :(

Here is my code, please give it a try:

ctrl

subtitlesApp.controller('AddSubtitleController',
    function($scope, addSubtitle) {
        $scope.saveSubtitle = function(subtitle, addSubtitleForm) {
            if (addSubtitleForm.$valid) {
                var x = addSubtitle.getTitle(subtitle.imdb_id);
                }
        };
    });

service

subtitlesApp.factory('addSubtitle', ['$http', function ($http) {
    return {
        getTitle: function(imdb_id) {
            $http({
                method: 'POST',
                url:'add_subtitle/',
                data: imdb_id,
            })
            .success(function(data, status, headers, config) {
                console.log(data);
            }).error(function(data, status, headers, config) {
                console.log(imdb_id+'eror')
            });
        }
    }
}]);

And here's what I'm getting when I print request.POST:

<QueryDict: {}>
4

1 回答 1

0

你应该让服务返回一个promise

subtitlesApp.factory('addSubtitle', ['$http', function ($http) {
    return {
        getTitle: function(imdb_id) {
            var promise = $http({
                method: 'POST',
                url:'add_subtitle/',
                data: imdb_id,
            })
            .success(function(data, status, headers, config) {
                console.log(data);
            }).error(function(data, status, headers, config) {
                console.log(imdb_id+'eror')
            });

            return promise;
        }
    }
}]);

何时消费服务

var x;
if (addSubtitleForm.$valid) {
    addSubtitle.getTitle(subtitle.imdb_id).then(function(data){
        x = data;
    });
}
于 2013-09-05T20:13:08.457 回答