Get empty model from backend in angularjs factory and update controller variable upon success

巧了我就是萌 提交于 2019-12-07 18:28:12

问题


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

            }]);

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:


This is what happens:

// 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

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.)


Solution 2:

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.



来源:https://stackoverflow.com/questions/22911354/get-empty-model-from-backend-in-angularjs-factory-and-update-controller-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!