Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从匿名函数返回JSON或任何值-AngularJS_Javascript_Angularjs_Json_Anonymous Function - Fatal编程技术网

Javascript 从匿名函数返回JSON或任何值-AngularJS

Javascript 从匿名函数返回JSON或任何值-AngularJS,javascript,angularjs,json,anonymous-function,Javascript,Angularjs,Json,Anonymous Function,在下面的代码中,我调用一个服务器方法“getUserName()”返回一个JSON并将其分配给main.teamMembers变量。我正在生成的报表中有一个viewAll按钮,它调用下面列出的方法“$scope.viewAllTeamMembers=function($event)”。 现在,view all按钮列出了main中存储的所有值。首次加载报告时,teamMembers不工作。但当我导航到报表中的其他按钮并尝试访问视图时,所有操作都会正常进行。 我询问从匿名用户返回JSON的原因是,我

在下面的代码中,我调用一个服务器方法“getUserName()”返回一个JSON并将其分配给main.teamMembers变量。我正在生成的报表中有一个viewAll按钮,它调用下面列出的方法“$scope.viewAllTeamMembers=function($event)”。
现在,view all按钮列出了main中存储的所有值。首次加载报告时,teamMembers不工作。但当我导航到报表中的其他按钮并尝试访问视图时,所有操作都会正常进行。
我询问从匿名用户返回JSON的原因是,我发现当它是一个全局变量时,viewAll按钮第一次起作用。请帮助我理解我所缺少的

angular.module('kamApp')
    .controller('MainCtrl', [
        '$q',
        '$rootScope',
        '$scope',
        '$timeout',
        'endpoints',
        'kamService',
        'queries',
        'translations',
        function($q, $rootScope, $scope, $timeout, endpoints, kamService, queries, translations) {

            var getUserNamesRequest = endpoints.getUserNames($rootScope.teamMemberIds).then(function(userDdata){
                return userDdata;
            });

            getUserNamesRequest.then(function(userDdata,$scope)  {

                $rootScope.userNameList = kamService.extractUserNameList(userDdata);

                main.teamMembers=kamService.concatTeamMemberName(
                    main.teamMembersData,
                    $rootScope.userNameList.list
                );

                main.teamMembers.list = kamService.sortList(main.teamMembers.list, 'role', 'name');  

            });
    }]);

--Directive

    angular.module('kamApp')
    .directive('teamMember', function() {

        return {
            templateUrl: 'views/team-member.html',
            replace: true,
            restrict: 'E',
            scope: {
                teamMembers: '=',
                viewSwitch: '=',
                changeReportTitle: '&'
            },
            link: function($scope) {

                $scope.itemLimit = 4;

                $scope.isOddLength = $scope.teamMembers.list.length % 2 !== 0;

                $scope.viewAllTeamMembers = function($event) {
                    $event.target.style.opacity = 0.6;
                    $scope.viewSwitch.dashboard = false;
                    $scope.viewSwitch.teamMember = true;
                    $scope.changeReportTitle()($scope.teamMembers.objectName.plural);
                };
            }
        };
    });
--HTML代码

 "<div class=\"expand-link inline-block-div\" ng-click=\"viewAllTeamMembers($event)\"> \n"+
“\n”+

在示例1中,您将
用户名设置为
getUserNames().then()
返回的承诺,而不是返回值和
JSON.stringify(用户名)在AJAX请求完成之前运行

在示例2中,
JSON.stringify(用户名)在AJAX请求完成之前运行

相反,您应该将依赖于
result
的代码放在then回调中

var result={};

getUserNames($rootScope.teamMemberIds).then(function(userDdata){
    result = userDdata;
    // Code dependent on result
});
如果要运行依赖于所设置变量的其他代码

var result={};

var getUserNamesRequest = getUserNames($rootScope.teamMemberIds).then(function(userData){
    result = userData;
    // Code dependent on result

    // return the data so that chained promises get the original data.
    return userData;
});

// Other code dependent on `result` being set.
getUserNamesRequest.then((userData) => {
    // use either `result` or `userData` here.
});

谢谢你的建议。我能够执行依赖于结果的代码,但在我的情况下,我希望在函数之外使用它,即我希望将结果分配给一个全局变量。这样做没有问题,您只需要确保访问变量的任何操作都会等待AJAX响应完成。请参阅更新的答案。@phuki我已经尝试了上述代码,但同样的问题仍然存在。我想全局存储结果。在下面的方法中,我们只能在函数中使用result或userData取决于设置的
结果的其他代码。getUserNamesRequest.then((userData)=>{//在此处使用
result
userData
)`@aravindswamy数据存储在“全局”变量中。您所遇到的问题是,在AJAX请求完成之前,它是不可用的,保证它可用的唯一方法是将promise与
then()
一起使用。如果您足够幸运,它可以在ajax请求之后运行,那么它可以与承诺链之外的代码一起工作completes@phuki谢谢你的信息,你是对的,花时间来完成。我检查了网络统计数据,发现服务器方法返回JSON。我已经更新了我正在编写的代码,希望你能看看。请不要像这样更新问题。请回滚编辑并询问另一个问题。