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

时间:2016-11-15 06:08:40

标签: javascript html angularjs

我有一个名为instructions.html的HTML页面。当我点击此页面中的按钮时,它应该naivgate到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>

这是我的控制器:

(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()方法。

2 个答案:

答案 0 :(得分:0)

您使用按钮类型作为提交,代码中没有表单标记。 使用表单标签然后实现它。

<form ng-submit="startQuiz(); closeThisDialog('button')">
<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">Start Quiz</button>
            </div>
        </div>
    </div>
</div>
</form>

答案 1 :(得分:0)

此代码存在几个问题。请在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语法不适用于您的代码,您可以在欢迎页面中指定控制器。所以以下就足够了:

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

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

我做了一些其他修改,只是为了使样本工作(即网址,删除缺少的依赖项)。请查看: - )