【问题标题】:$scope assignment works in one case but in other$scope 分配在一种情况下有效,但在另一种情况下有效
【发布时间】:2016-10-31 10:27:38
【问题描述】:

第一个案例

angular.module('tss.application').controller("UserspaceController",  function($scope, $http)
{
        $http(
            {
                url     : "/dirlist",
                method  : "GET",
            }).then(function successCallback(response) 
            {
                $scope.lists = response;
            }, 
            function errorCallback(response) 
            {
                window.alert("Dir list could not be get");
            });

});

第二种情况

angular.module('tss.application').controller("UserspaceController", function ($scope, $http) 
    {
        $http.get('dirlist').success(function(data) 
        {
            $scope.lists = data;
        }); 
    });

我对 Angularjs 很陌生,所以这可能是一个愚蠢的问题。反正, 列表变量的分配在第二种情况下有效,但在第一种情况下有效。也就是说,第二个可以访问控制器内“列表”的值。我没看懂第一种情况有什么问题?

【问题讨论】:

    标签: angularjs angular-controller


    【解决方案1】:
    angular.module('tss.application').controller("UserspaceController",  function($scope, $http)
    {
            $http(
                {
                    url     : "/dirlist",
                    method  : "GET",
                }).then(function successCallback(response) 
                {
                    $scope.lists = response.data;
                }, 
                function errorCallback(response) 
                {
                    window.alert("Dir list could not be get");
                });
    
    });
    

    put $scope.lists = response.data;,会起作用的

    【讨论】:

      【解决方案2】:

      试试这个:

      angular.module('tss.application').controller("UserspaceController",  function($scope, $http)
      {
              $http(
                  {
                      url     : "/dirlist",
                      method  : "GET",
                  }).then(function successCallback(response) 
                  {
                      $scope.lists = response.data;
                  }, 
                  function errorCallback(response) 
                  {
                      window.alert("Dir list could not be get");
                  });
      
      });
      

      已弃用的 success() 方法为数据和标头传递两个单独的值,但使用 .then() 的承诺接口仅传递一个 response 值,该值具有数据和标头作为属性。

      对您的代码的更改只是以下行:

                      $scope.lists = response.data;
      

      【讨论】:

        猜你喜欢
        • 2017-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        • 2018-09-15
        • 1970-01-01
        相关资源
        最近更新 更多