angularJS-无法从http获取变量的值 [英] angularJS- unable to obtain value for a variable from http

查看:91
本文介绍了angularJS-无法从http获取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a class="clickme" ng-click="vm.open()" >Click ME</a>

在我的HTML代码中,我具有上面给出的链接,单击该链接会打开一个模式弹出窗口.该模式弹出窗口与其自身的控制器相关联.因此,当调用此链接时,我必须传递通过http请求获得的值.这就是我的控制器的外观;

In my HTML code, I have a link as given above, clicking on which opens a modal popup. This modal popup is associated with a controller of it's own. So, when this linked is called, I have to pass a value which I obtain through a http request. This is how my controller looks like;

(function() {

angular
    .module('myApp.abc', [])

    .factory('myService', function($http) {
    })

    .controller('MyController', function($routeParams, myService, $scope, $uibModal,$http) {
        var vm = this;

        vm.open = function () {
            var modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'path to modal popup',
                controller: 'modalPopController',
                resolve: {
                    id: function () 
                    {
                        var myid;
                        var baseUrl = 'services/';
                        $http.get(baseUrl+ 'get_user_session')
                        .then(function(response) {
                            myid = response.id;
                            return myid;
                        });
                    }
                }
            });

            modalInstance.result.then(function (myCroppedImage) {
                vm.member.avatar = myCroppedImage;
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        };

    });
})();

在此控件中,这是我与服务进行通信的部分,该服务返回一个我试图分配给变量id的值.这是该代码:

In this controoler, this is the part where I'm communicationg with a service which returns a value which I'm trying to assign to the variable id. This is that code:

var modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'path to modal popup',
                controller: 'modalPopController',
                resolve: {
                    id: function () 
                    {
                        var myid;
                        var baseUrl = 'services/';
                        $http.get(baseUrl+ 'get_user_session')
                        .then(function(response) {
                            myid = response.id;
                            return myid;
                        });
                    }
                }
            });

但是我收到未定义的错误.那是什么?

But I'm getting undefined error. What is that so?

更新

如果我这样放置一个静态值,它就可以正常工作.

If I put a static value like this, it works fine.

resolve: {
                    id: function () {
                        return 5;
                    }
                }

我的服务正在以JSON的形式返回结果:

My service is returning result as JSON like this:

{"id":"5","name":"John"}

推荐答案

我不确定这是否对您有用,但是您可以尝试类似的方法

I am not sure if this is going to work for you but you could try something like this

var modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'path to modal popup',
                controller: 'modalPopController',
                resolve: {
                    id: function () 
                    {
                        var myid;
                        var baseUrl = 'services/';
                        var defer = $q.defer();
                        $http.get(baseUrl+ 'get_user_session')
                        .then(function(response) {
                            myid = response.id;
                           defer.resolve(myid);
                        });
                    return defer.promise;
                  }
                }
            });

这篇关于angularJS-无法从http获取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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