Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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
Javascript AngularJS在$http中未定义_Javascript_Angularjs_Http - Fatal编程技术网

Javascript AngularJS在$http中未定义

Javascript AngularJS在$http中未定义,javascript,angularjs,http,Javascript,Angularjs,Http,我目前正在做一个小AngularJS项目 我在我的服务中得到了这个代码 testApp.service('testService',['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http){ this.getDetails= function(name){ $http({ method : "GET",

我目前正在做一个小AngularJS项目

我在我的服务中得到了这个代码

    testApp.service('testService',['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http){

    this.getDetails= function(name){

        $http({
            method : "GET",
            url : "url"       
        }).then(function mySucces(response) {
           return response.data;
        }, function myError(response) {
            $scope.myWelcome = response.statusText;
        });

    };

}]);
我正在从控制器中的服务调用该方法:

testApp.controller('mainController', ['$scope', '$http', 'testService', function($scope, $http, testService){

    $scope.details;

    $scope.loadDetails= function(val){
        $scope.details= testService.getDetails(val);
        console.log($scope.details);
        console.log(val);
    };

}]);
$scope.loadDetails= function(val) {
    testService.getDetails(val).then(function(data) {
        $scope.details = data;
    });        
};
当用户在搜索字段中键入内容时,将在我的视图中调用$scope.loadDetails。因此,它必须自动更新下拉列表

服务中的response.data返回一个数组。 但是控制器中的$scope.details保持未定义状态。有人知道如何将response.data从服务返回给控制器吗


我已经尝试返回$http之外的response.data,但我在控制器中一直没有定义。

您处理承诺的方式错误。这一行:

$scope.details=detailsService.getDetails(val)
detailsService.getDetails
的返回值分配给
$scope.details
。目前,
detailsService.getDetails
没有返回任何内容,因此您会得到
undefined
。如果必须在服务中处理响应,则可以执行以下操作:

return $http({
  method : "GET",
  url : "url"      
})`

您正在返回
$http
承诺。

您以错误的方式处理承诺。这一行:

$scope.details=detailsService.getDetails(val)
detailsService.getDetails
的返回值分配给
$scope.details
。目前,
detailsService.getDetails
没有返回任何内容,因此您会得到
undefined
。如果必须在服务中处理响应,则可以执行以下操作:

return $http({
  method : "GET",
  url : "url"      
})`

您正在返回
$http
承诺。

通过$http进行的通信是异步的,并且通过承诺工作。 调用
$http
返回一个承诺,然后
调用也返回一个承诺。
如果在那里添加
return response.data
,则生成的承诺将通过数据数组得到解决。您还需要
返回
来自
getDetails
函数的承诺,因此添加
return$http(…

现在,在控制器中,当您调用
testService.getDetails()时
您将获得承诺。但这不是结果本身,因此将返回值分配给作用域没有意义。相反,您需要调用另一个
。然后使用回调函数对该承诺执行
,该函数将值分配给
$scope

在职:

this.getDetails= function(name){

    return $http({
        method : "GET",
        url : "url"       
    }).then(function (response) {
       return response.data;
    });

};
在控制器中:

testApp.controller('mainController', ['$scope', '$http', 'testService', function($scope, $http, testService){

    $scope.details;

    $scope.loadDetails= function(val){
        $scope.details= testService.getDetails(val);
        console.log($scope.details);
        console.log(val);
    };

}]);
$scope.loadDetails= function(val) {
    testService.getDetails(val).then(function(data) {
        $scope.details = data;
    });        
};

通过$http的通信是异步的,通过承诺工作。 调用
$http
返回一个承诺,然后
调用也返回一个承诺。
如果在那里添加
return response.data
,则生成的承诺将通过数据数组解析。您还需要
return
getDetails
函数中返回承诺,因此添加
return$http(…

现在,在控制器中,当您调用
testService.getDetails()时
您将获得承诺。但这不是结果本身,因此将返回值分配给作用域没有意义。相反,您需要调用另一个
。然后使用回调函数对该承诺执行
,该函数将值分配给
$scope

在职:

this.getDetails= function(name){

    return $http({
        method : "GET",
        url : "url"       
    }).then(function (response) {
       return response.data;
    });

};
在控制器中:

testApp.controller('mainController', ['$scope', '$http', 'testService', function($scope, $http, testService){

    $scope.details;

    $scope.loadDetails= function(val){
        $scope.details= testService.getDetails(val);
        console.log($scope.details);
        console.log(val);
    };

}]);
$scope.loadDetails= function(val) {
    testService.getDetails(val).then(function(data) {
        $scope.details = data;
    });        
};

您是否在控制器中注入了服务??您是否可以为服务和控制器添加依赖项列表,我怀疑您没有注入什么\您是否在控制器中注入了服务??您是否可以为服务和控制器添加依赖项列表,我怀疑您没有注入什么\如果我理解正确,即使我服务中的se return response.data仍然返回promes?现在您没有返回承诺。http是异步的,您的response.data返回得比较晚。当时getDetails函数已返回undefined ALREADYI如果我理解正确,即使我在服务中使用return response.data,它仍然返回一个promespromes?现在你没有返回承诺。http是异步的,你的响应。数据返回得很晚。当时getDetails函数返回了未定义的alreadyIt终于工作了!非常感谢你的好解释。今天救了我。谢谢!终于工作了!非常感谢你的好解释。保存了me今天,谢谢你!