javascript - 单击 ng-dialog 中的按钮不起作用

标签 javascript html angularjs

我有一个名为 instructions.html 的 HTML 页面。当我单击此页面中的按钮时,它应该导航到 quiz-list.html 并使用“开始测验”按钮打开 ng-dialog。当我单击此按钮时,弹出窗口应该会消失并开始测验。但是点击按钮并没有发生。

下面是 instructions.html 页面。

<div class="container-fluid" ng-controller="KbcQuizController">
    <div class="instructions-container">
        <div class="instructions-heading">
            <span class="header-text"> Rules </span>
        </div>
        <div>
            <p>1. Persons must enter the Competition on their own behalf and
                entry(ies) by proxy will not be accepted, even for their family
                members.</p>
            <p>2. An entry/ies is not transferrable.</p>
            <p>3. The Company or the Producers will not entertain any claims
                / questions / queries with respect to the authenticity or
                correctness of any questions and answers for the questions asked in
                any round of the Competition.</p>
            <p>4. The Company’s decision on the correctness or incorrectness
                of any answer is final and binding on all Contestants.</p>
            <p>5. Use of mobile phones will not be permitted during the
                shoot, and during the Auditions. It may lead to disqualification.</p>
        </div>
        <div class="next-container">
            <button class="next-button btn btn-primary" ng-click="getWelcomePage()">Start Quiz</button>
        </div>
    </div>
</div>

这是我的 Controller :

(function() {

    'use strict';

    angular.module('app.kbcquiz').controller('KbcQuizController',KbcQuizController);

    KbcQuizController.$inject = [ '$timeout', '$rootScope', '$scope', '$http','$filter', 'ngDialog', 'usSpinnerService', 'quizService', '$state' ];
    function KbcQuizController($timeout, $rootScope, $scope, $http, $filter,ngDialog, usSpinnerService, quizService, $state) {
        console.log("quizService is::" + quizService);
        $scope.count = 1;
        $scope.timer = {
            value : 60
        }

        var stopped;

        $scope.startTimer = function() {
            stopped = $timeout(function() {
                console.log($scope.timer.value);
                $scope.timer.value--;
                $scope.startTimer();
                if ($scope.timer.value == 0) {
                    alert("timeout");
                }
            }, 1000);
        };

        $scope.stop = function() {
            $timeout.cancel(stopped);

        }

        $scope.reset = function() {
            $scope.timer.value = 60;
        }

        $scope.startQuiz = function() {
            console.log("in start quiz");
            quizService.getQuestion($scope.count).then(null, function(err) {
                console.log("error in get question");
            });
            $scope.startTimer();
        }

        $scope.getWelcomePage = function() {
            $state.go('quizpage');
            ngDialog
                    .open({
                        template : 'app/modules/kbcquiz/welcome.html',
                        className : 'ngdialog-theme-default',
                        controller : KbcQuizController,
                        controllerAs : 'vm',
                        scope : $scope,
                    });
        }

    }

})();

这是我的模块:

(function() {
    'use strict';
    var module = angular.module('app.kbcquiz', [ 'ui.router','angularUtils.directives.dirPagination', 'ng-bootstrap-datepicker','ngDialog', 'angularSpinner' ]);

    module.config(appConfig);

    appConfig.$inject = [ '$stateProvider' ];

    function appConfig($stateProvider) {
        $stateProvider.state('app.kbcquiz', {
            url : '/rules',
            templateUrl : 'app/modules/kbcquiz/instructions.html',
        })

        .state('quizpage', {
            url : '/app/kbc-quiz',
            templateUrl : 'app/modules/kbcquiz/quiz-list.html',

        });

    }
})();

这是我的 welcome.html:

<h3 class="dialog_header">Welcome to KBC!!</h3>
<div class="dialog-contents" ng-controller="KbcQuizController">
    <div class="ngdialog-message">
        <div>
            <div class="next-button">
                <button type="submit"
                    class="ngdialog-button ngdialog-button-primary"
                    ng-click="startQuiz(); closeThisDialog('button')">Start Quiz</button>
            </div>
        </div>
    </div>
</div>

请让我知道我要去哪里错了。因为当我点击 ng-dialog 中的按钮时,它甚至不会进入 startQuiz() 方法。

最佳答案

这段代码有几个问题。请在 http://embed.plnkr.co/nNMsoxHgP1ZUPizf8nIY/ 查看工作示例

  • 您不需要 type='submit' 按钮,除非您确实要发布一些数据。在您的情况下, type='button' 就足够了。不过这不会造成任何问题。

    <button type="button" class="ngdialog-button ngdialog-button-primary" 
            ng-click="startQuiz(); closeThisDialog('button')">Start Quiz
    </button>
    
  • 您的 ngDialog 调用是一个问题。 controllerAs 语法不适用于您的代码,您确实在欢迎页面中指定了 Controller 。所以以下就足够了:

    ngDialog
    .open({
            template : 'welcome.html',
            className : 'ngdialog-theme-default'
    });
    
  • 另一个问题是您声明了一个名为 app.kbcquiz 的状态。这实际上意味着您应该有一个名为 app 的状态,而 kbcquiz 将是 app 的嵌套状态。对于示例,我只是将状态重命名为 instructions

    $stateProvider.state('instructions', {
        url : '',
        templateUrl : 'instructions.html'
    })
    

我做了一些其他的改编,只是为了让示例工作(即 url,删除缺失的依赖项)。请检查一下:-)

关于javascript - 单击 ng-dialog 中的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40603146/

相关文章:

html - 悬停语法之间的区别

javascript - 单击 div 而不是文件输入标签时打开文件选择

javascript - NodeJS - Electron 托盘图标一分钟后消失

javascript - 加载页面上的所有元素 Selenium + Ruby

HTML 表单不作为一个连接

javascript - Angularjs ng-if函数被多次调用

javascript - 如何创建在 Angular promise 中捕获的错误

javascript - 防止 Slider Revolution 暂停 HTML5 视频

javascript - 当有一组 div 时使用 fadeToggle + 淡出所有其他内容

html - 如何使用JQuery控制基于CSS的SVG动画?