从 angularjs 工厂的后端获取空模型并在成功后更新控制器变量 [英] Get empty model from backend in angularjs factory and update controller variable upon success

查看:21
本文介绍了从 angularjs 工厂的后端获取空模型并在成功后更新控制器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工厂,在我的工厂 http 调用中,我正在为我的用户变量赋值,但它没有更新我的控制器.

This is my factory and in my factory http call I am assigning value to my User variable but it is not updating my controller.

mainApp.factory('AccountFactory', ['$http', function ($http) {
                var User = {

                };

                function getLogOnModel() {
                    $http.get("http://localhost/mylocalspecial/Account/LogOn").then(function (data) {
                        User = data.data;
                        alert(JSON.stringify(data.data));
                        return data;
                    });
                }

                // Init model (or leave it for the controller to init it)
                getLogOnModel();

                return {
                    User: User,
                    getLogOnModel: getLogOnModel
                };

            }]);

这是我的控制器,正如您在我的控制器中看到的,我将工厂用户变量分配给 $scope.logOnModel 但我的控制器没有更新.

This is my controller as you can see in my controller I am assigning factory User variable to $scope.logOnModel but my controller is not updating.

mainApp
        .controller('AccountController', ['$scope', 'AccountFactory',
            'AuthenticationServiceFactory'
            , function ($scope, AccountFactory,
                AuthenticationServiceFactory) {

                $scope.logOnModel = {};
                $scope.customerModel = {};

                $scope.logOnModel = AccountFactory.User;
                alert(JSON.stringify(AccountFactory.User));



            }]);

推荐答案

这是发生了什么:

// 1
var User = {};   // local User variable references new object: #obj1

// 2
return { 
    User: User   // AccountFactory.User references same object: #obj1
    ...
}

// 3
User = data.data;   // local User variables references new object: #obj2
                    // AccountFactory.User still references old object: #obj1
                    // $scope.logOnModel still references AccountFactory.User
                    //     a.k.a. old object: #obj1


解决方案 1:

不要重新分配整个对象(User = ),只需重新分配其属性:


Solution 1:

Do not reassign the whole object (User = <some_new_object>), just reassign its properties:

User.prop1 = data.data.prop1;
User.prop2 = data.data.prop2;
...

(如果属性不止一对,这很乏味且容易出错.)

(This is tedious and error-prone if the properties are more than a couple.)

您可以将整个服务分配给一个范围属性并从该范围中引用它.

You could assign the whole service to a scope property and reference it from the scope.

mainApp.factory('AccountFactory', ['$http', function ($http) {
    var service = {};
    service.User = {};
    service.getLogOnModel = function () {
        $http.get("...").success(function (data) {
            service.User = data;
            alert(JSON.stringify(data));
        });
    };

    // Init model (or leave it for the controller to init it)
    service.getLogOnModel();

    return service;
}]);

mainApp.controller('AccountController', ['$scope', 'AccountFactory',
    function ($scope, AccountFactory) {
        $scope.account = AccountFactory;
        ...
    }
]);

// In HTML:
{{account.User.EmailAddress}}

另请参阅此简短演示.

See, also, this short demo.

这篇关于从 angularjs 工厂的后端获取空模型并在成功后更新控制器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆