小编Atu*_*ary的帖子

更新Angular工厂变量时更新控制器变量

嗨,我有一个问题.我的工厂里有一个对象如下

User: {
   EmailAddress: ""
}
Run Code Online (Sandbox Code Playgroud)

每当我进行http调用时,我想更新User.EmailAddress whith返回的值.在工厂内做这件事的最佳方法是什么?所以在控制器级别我可以将$ scope.Email绑定到工厂变量.这就是我现在正在做的事情

GetLogOnModel: function () {
    if ($location.path().indexOf("login") == 1) {
        var promise = $http.get(config.headers.url + "LogOn").then(function (response) {
            // The return value gets picked up by the then in the controller.
            User.EmailAddress=response.data.Email;
            return response.data
        });
        return promise;
        // Return the promise to the controller
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制器中

AccountFactory.GetLogOnModel().then(function (data) {
  $scope.logOnModel = data;
}, function (err) {
  console.log(err.reason);
  alert(err.reason);
});
Run Code Online (Sandbox Code Playgroud)

angularjs

4
推荐指数
2
解决办法
1万
查看次数

标签 统计

angularjs ×1