0

I'm updating an object property in a factory and it doesn't seem to be updating the property referenced in the controller.

Say I have a factory that looks something like this...

app.factory('User', function ($http)
{  
 $http.defaults.useXDomain = true;

    var User = {
        Name: "",
        JobTitle: "",
        isloaded: false
    };

User.GetUser = function (ID)
{
       $http(
            {
                url: "webservice uri",
                method: "GET"
            }
        ).success(function(data, status, headers, config)
        {               
            User.isloaded= true;

        }).error(function(data, status, headers, config)
        {

        })      
}
return User;
});

the property 'isloaded' is initially set to false, and then once the $http calls goes through the success callback, it is eventually set to false.

And then I have a controller that looks something like

controllers.DefaultCtrl = function($scope, User)
{ 
    $scope.showMessage = User.isloaded;
    $scope.getUser = function ()
    {
        User.GetUser("11111");
    }
    $scope.getUser();   
    }

which is associated with this html

<h1>{{showMessage.isloaded ? 'success' : 'Loading...'}}</h1>

Ultimately I would like it to say 'success', however, the text never changes in the html

4

1 回答 1

0

问题是您正在分配一个原语:

 $scope.showMessage = User.isloaded;

showMessage被赋值为false,它不是“指向”isloaded属性。

您可能应该做的是将 设置User为范围变量,例如:

controllers.DefaultCtrl = function($scope, User)
{ 
  $scope.user = User;
  $scope.user.getUser("11111");   
}

然后在您的html中您可以执行以下操作:

<h1>{{user.isloaded ? 'success' : 'Loading...'}}</h1>
于 2014-05-14T22:13:10.637 回答