Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular service在执行http post时不返回控制器中的任何数据_Angularjs - Fatal编程技术网

Angularjs Angular service在执行http post时不返回控制器中的任何数据

Angularjs Angular service在执行http post时不返回控制器中的任何数据,angularjs,Angularjs,Angular service在执行http post时不会在控制器中返回任何数据 app.controller("letter_active", function ($scope, myService) { $scope.artist = myService.getReplyType2(); }); app.service('myService', function ($http) { this.getReplyType2 = function () { $

Angular service在执行http post时不会在控制器中返回任何数据

app.controller("letter_active", function ($scope, myService) {
    $scope.artist = myService.getReplyType2();
});

app.service('myService', function ($http) {

    this.getReplyType2 = function () {
        $http.post('file/getReplyType', {})
        .success(function (data, status) {
            return data;
        }).error(function (data, status) {
            alert("Error has occured" + status);
        });
    }
});

angular方面的新手需要帮助吗?

angular中异步服务的典型工作是服务返回承诺对象,在controller中,您使用此承诺的
然后/success
方法设置范围数据:

app.controller("letter_active", function ($scope, myService) {
    myService.getReplyType2().then(function(data) {
        $scope.artist = data;    
    });
});

app.service('myService', function ($http) {

    this.getReplyType2 = function () {
        return $http.post('file/getReplyType', {}).success(function (data, status) {
            return data;
        }).error(function (data, status) {
            alert("Error has occured" + status);
        });
    }
});

请注意,
getReplyType2
方法现在返回
$http.post
调用的结果,这是一个Promise对象。

对于异步调用,您需要返回Promise

this.getReplyType2 = function() {
    var defer = $q.defer();
        $http({method: 'POST', url: 'file/getReplyType', data: {}})
            .success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                defer.resolve(data);
                    })
            .error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                window.data = data;
            });

    return defer.promise;
}

app.controller("letter_active", function ($scope, myService) {
    myService.getReplyType2().then(function(data) {
        $scope.artist = data;    
    });
});

如果你希望人们从一天中抽出时间帮助你恢复数据,请至少检查你的语法和标点符号@user2601893应该是
myService.getReplyType2()。然后
,我想这是一个输入错误。感谢dfsk,它返回的数据很好。。。但另一个问题是,它没有绑定到ng repeat或
ng options=“a.cmn\u name for a in artist”
。调用它时,如何调用
getReplyType2