2016-10-31 58 views
0

首例工作

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所以这可能是一个愚蠢的问题。无论如何, 列表变量的作品在第二种情况下,但在第一。也就是说,第二个可以访问控制器内的“列表”的值。我不明白第一个案件有什么问题?

回答

0

尝试这种情况:

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; 
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"); 
      }); 

}); 

把$ scope.lists = response.data ;,将工作

相关问题