$ scope赋值在一个案例中有效但在另一个案例中有效

时间:2021-04-19 15:00:39

First Case

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

});

Second Case

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

I am very new to Angularjs so this could be a stupid questions. Anyway, the assignment of lists variable works in second case but in first. That is, the second can access the values of "lists" inside the controllers. I failed to understand what is wrong with the first case?

我对Angularjs很新,所以这可能是一个愚蠢的问题。无论如何,列表变量的赋值在第二种情况下起作用,但在第一种情况下。也就是说,第二个可以访问控制器内的“列表”的值。我不明白第一个案子有什么问题?

2 个解决方案

#1


0  

Try this:

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

});

The deprecated success() method passes two separate values for data and headers, but the promise interface using .then() passes only a single response value which has the data and headers as attributes.

不推荐使用的success()方法为数据和标头传递两个单独的值,但使用.then()的promise接口仅传递一个响应值,该值具有数据和标头作为属性。

The change to your code is simply the line:

对代码的更改只是行:

                $scope.lists = response.data;

#2


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;, will work

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

#1


0  

Try this:

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

});

The deprecated success() method passes two separate values for data and headers, but the promise interface using .then() passes only a single response value which has the data and headers as attributes.

不推荐使用的success()方法为数据和标头传递两个单独的值,但使用.then()的promise接口仅传递一个响应值,该值具有数据和标头作为属性。

The change to your code is simply the line:

对代码的更改只是行:

                $scope.lists = response.data;

#2


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;, will work

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