首页 > 解决方案 > 在 angularjs 中第一次调用 Javascript 函数时不起作用

问题描述

我基本上是在 HTML 按钮单击中调用 $scope.ShowQuestionDetail 函数。当我第一次单击 $scope.questionJson 值是空的,因为 getquestionData() 函数不起作用。当我第二次单击时,它工作正常。

  $scope.ShowQuestionDetail = function (questionID, displayEditLink, $index) {
        try {
            if ($scope.rdConceptExemplarOn == true && $scope.conceptExemplarVisible == true) {
                $scope.IsCBC = true;
            } else {
                $scope.IsCBC = false;
            }
            var scrollToElement = '#avfq' + questionID;
            var questionTextElement = '#avfqQT' + questionID;
            $scope.displayEditLink = displayEditLink;
            $scope.questionID = questionID;
            getquestionData();
            var isQuestionExist = ($scope.QuestionAdded[$index] === undefined || !$scope.QuestionAdded[$index]) ? false : true;
            var promise = AssessmentBuilderServices.IsPilotInstitution($scope.institutionId);
            promise.then(function (results) {
                $scope.isCABPilotInstitution = results.data.IsPilotInstitutionResult.IsPilot;

                if ($scope.isCABPilotInstitution && $scope.questionJson) {

                    selectedTemplate = 'App/Components/Assessments/Questions/ViewFullQuestionIE.html';
                    selectedController = 'ViewFullQuestionIEController';
                    $scope.ViewFullQuestionIEModal(selectedTemplate, selectedController, questionID, isQuestionExist);

                }
                else {
                        // code for redirection to other controller
                }

            }, function (results) {
                    toaster.pop({ type: 'error', title: '', body: 'Error encountered while fetching pilot institution.', toastId: 'ShowQuestionDetail' });
            });

        }
        catch (err) {
            toaster.pop({ type: 'error', title: '', body: 'Error occurred while opening question details.', toastId: 'ShowQuestionDetail' });
        }
    }

                function getquestionData() {
            var searchText = '';
            var promise = AssessmentBuilderServices.GetQuestionDetail('Deprecated', $scope.questionID);
            promise.then(function (results) {

                if (results.data.GetQuestionDetailResult.IsError) {
                    toaster.pop({ type: 'error', title: '', body: results.data.GetQuestionDetailResult.ErrorMessage, toastId: 'getquestionData' });
                }
                else {
                    $scope.questionJson = results.data.GetQuestionDetailResult.QuestionJson;
                    $scope.questionTypeText = results.data.GetQuestionDetailResult.QuestionTypeText;

                }
            }, function (results) {
                toaster.pop({ type: 'error', title: '', body: 'Error occured while retrieving questionData information.', timeout: 4000, toastId: 'getquestionData' });
            });
        }

标签: javascriptangularjs

解决方案


推荐阅读