Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 在Angular JS中,从$http分配$scope变量值_Javascript_Angularjs - Fatal编程技术网

Javascript 在Angular JS中,从$http分配$scope变量值

Javascript 在Angular JS中,从$http分配$scope变量值,javascript,angularjs,Javascript,Angularjs,我是新手。这里我有一个代码:我收到响应数据,比如number。在这段代码中,我如何将响应数据分配为$scope.vote\u counting。在此代码中,不返回任何内容 $scope.votes = function(){ var votes = $http({ method: "post", url: "/getVotes", data: { id: $scope.Id}

我是新手。这里我有一个代码:我收到响应数据,比如number。在这段代码中,我如何将响应数据分配为$scope.vote\u counting。在此代码中,不返回任何内容

$scope.votes = function(){
        var votes = $http({
              method: "post",
              url: "/getVotes",
              data: { id: $scope.Id}
            }).success(function(response){  
            });
          return votes;
    }

请任何人帮忙。

只需拨打
$http
。它不必在函数中

$http({
    method: "post",
    url: "/getVotes",
    data: { id: $scope.Id }
}).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error){
    //handle error
});
排序版本是

$http.post("/getVotes", { id: $scope.Id }).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error) {
    //handle error
})

注意:您使用的是POST方法,但GET方法似乎更适合您的情况(
getvowers

只需调用
$http
。它不必在函数中

$http({
    method: "post",
    url: "/getVotes",
    data: { id: $scope.Id }
}).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error){
    //handle error
});
排序版本是

$http.post("/getVotes", { id: $scope.Id }).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error) {
    //handle error
})

注意:您使用的是POST方法,但GET方法似乎更适合您的情况(
getVoces

此$http函数不返回服务器的响应。但是,正如您已经发现的,您可以使用success函数来获取服务器响应。只需在success函数中设置
$scope.vows
值,如下所示:

 $http({
   method: "post",
   url: "/getVotes",
   data: { id: $scope.Id}
 }).success(function(response){  
   $scope.votes = response
 });

$http函数不返回服务器的响应。但是,正如您已经发现的,您可以使用success函数来获取服务器响应。只需在success函数中设置
$scope.vows
值,如下所示:

 $http({
   method: "post",
   url: "/getVotes",
   data: { id: $scope.Id}
 }).success(function(response){  
   $scope.votes = response
 });

最简单的可能是使用。请注意,
success
被弃用,取而代之的是
then

$scope.retrieveVotes = function(){
  $http.post('/getVotes', {id : $scope.id}).then(function(response){  
    $scope.votes = response.data;
  });
}

还要注意,
$http
调用是异步的,因此调用
retrievevows
也是异步的

最简单的方法可能是使用。请注意,
success
被弃用,取而代之的是
then

$scope.retrieveVotes = function(){
  $http.post('/getVotes', {id : $scope.id}).then(function(response){  
    $scope.votes = response.data;
  });
}

还要注意,
$http
调用是异步的,因此调用
retrievevows
也是异步的

我添加了一个片段,展示了对承诺的基本处理。在这里,我使用了一个服务来模拟http调用。响应附加到范围变量,该变量显示在视图中

角度模块('TestApp',[]) .factory('MockHttp',函数($q){ 返回{ getMockData:函数(){ 返回$q.when(['A','B','C']); } }; }) .controller('TestController',函数($scope,MockHttp){ $scope.res=null; MockHttp.getMockData() .然后(功能(res){ $scope.res=res; }) .catch(函数(err){ 控制台日志(err); }); });

{{res}}

我添加了一个片段,展示了承诺的基本处理。在这里,我使用了一个服务来模拟http调用。响应附加到范围变量,该变量显示在视图中

角度模块('TestApp',[]) .factory('MockHttp',函数($q){ 返回{ getMockData:函数(){ 返回$q.when(['A','B','C']); } }; }) .controller('TestController',函数($scope,MockHttp){ $scope.res=null; MockHttp.getMockData() .然后(功能(res){ $scope.res=res; }) .catch(函数(err){ 控制台日志(err); }); });

{{res}}

为什么
$http.post()
?@Weedoze是因为它是
$http({method:'post',..)的快捷方式
使你的代码更可读、更简洁哦,是的,我没有看到他在使用post…即使url是
获取投票
为什么
$http.post()
?@Weedoze是因为它是
$http({method:'post',..)的快捷方式
使您的代码更具可读性和简洁性哦,是的,我没有看到他在使用帖子……即使url是
getvows