无法将数据传递给angularjs中的数据对象

时间:2016-05-20 05:53:26

标签: javascript angularjs

N.B:我对angularJS编程非常陌生。我想要做的是,将服务返回的信息保存到对象中。我的对象看起来像这样。

var userObject = {
                    "id": "1",
                    "firstName": "Amelia",
                    "lastName": "Earheart"
                };

我有一个从后端返回数据的工厂,它看起来像这样:

.factory('myService', function($http) {
            var baseUrl = 'backendservice/';

            return {
                myInfo:function() {
                    return $http.get(baseUrl + 'getmyInfo');
                }
            };
        })

这就是我的控制器与工厂服务通信的方式:

    .controller('myController', function($routeParams,myService) {
        var my = this;

        my.basicInfo = function () {
                //to get my info
                myService.myInfo().success(function(data) {
                    my.activeUser.myData.id             = data.id;
                    my.activeUser.myData.firstName  = data.firstName;
                    my.activeUser.myData.lastName   = data.lastName;
                }); 
        };


        my.addSomething = function(post) {
            var userObject = my.basicInfo();

        };

    });

这就是我将数据分配到userObject

的方式
var userObject = my.basicInfo();

我不知道为什么它不起作用。工厂服务运行但该值未分配给userObject。

我的控制器整体看起来像这样:

(function() {
    angular
        .module('myApp.spa', [])

        .factory('myService', function($http) {
            var baseUrl = 'backendservice/';

            return {
                myInfo:function() {
                    return $http.get(baseUrl + 'getmyInfo');
                }
            };
        })

        .controller('myController', function($routeParams,myService) {
            var my = this;

            my.basicInfo = function () {
                    //to get my info
                    myService.myInfo().success(function(data) {
                        my.activeUser.myData.id             = data.id;
                        my.activeUser.myData.firstName  = data.firstName;
                        my.activeUser.myData.lastName   = data.lastName;
                    }); 
            };


            my.addSomething = function(post) {
                var userObject = my.basicInfo();

            };

        });         
})();

3 个答案:

答案 0 :(得分:3)

您的函数my.basicInfo()不会返回任何内容,因此变量userObject的值为undefined。此外,如果您想在视图上使用userObject,请在控制器实例上将其公开为my.userObject

如果您要为userObject分配值,请在success方法的my.basicInfo()回调中执行,或者从方法my.basicInfo()返回承诺并指定then回调承诺的价值

方法1

    my.basicInfo = function () {
            //to get my info
            var activeUser = {};
            return myService.myInfo()
            .then(function(response) {
                angular.extend(activeUser, response.data);
                my.userObject = activeUser;
            }); 
    };

方法2

    my.basicInfo = function () {
            //to get my info
            var activeUser = {};
            return myService.myInfo()
            .then(function(data) {
                angular.extend(activeUser, response.data);
                return activeUser;
            }); 
    };

    my.addSomething = function(post) {
        my.basicInfo()
         .then(function (response) {
           my.userObject = response;
         });
    };

答案 1 :(得分:0)

原因是my.basicInfo没有返回任何内容并且也从$ http.success / failure返回,您无法返回任何值。 因此,在这种情况下,您需要执行以下步骤:

  1. 在控制器顶部定义var userObject,以便所有方法都可以访问。
  2. 在$ http

    的成功回调中将数据分配给userObject
    (function() {
    angular
        .module('myApp.spa', [])
    
        .factory('myService', function($http) {
            var baseUrl = 'backendservice/';
    
            return {
                myInfo:function() {
                    return $http.get(baseUrl + 'getmyInfo');
                }
            };
        })
    
        .controller('myController', function($routeParams,myService) {
            var my = this;
            var userObject;
    
            my.basicInfo = function () {
                    //to get my info
                    myService.myInfo().success(function(data) {
                        my.activeUser.myData.id             = data.id;
                        my.activeUser.myData.firstName  = data.firstName;
                        my.activeUser.myData.lastName   = data.lastName;
    
                        userObject = data;
                    }); 
            };
    
    
            my.addSomething = function(post) {
                my.basicInfo();
    
            };
    
        });         
    })();
    

答案 2 :(得分:-1)

.factory('UserInfo', function($resource, apiHost) {
  return $resource(apiHost + '/userinfo/:userId'); 
});


.controller('myController', function($routeParams,UserInfo) {
        var vm = this;
        // suppose that you have stored the userId somewhere after the login
        vm.userObject = {};
        var myUserInfo = UserInfo.get({
                userId: userId
            });



             vm.refreshData = function (){
              myUserInfo.$promise
                .then(function(response) {
                 vm.userObject = response;

                }, function(error) {

                    // doSomething
                });
              };

            vm.update = function(){
               myUserInfo.save(vm.userObject, function() {
                // console.log('success');

               }, function(error) {
                // console.log('error');

              });


        };

    });