0

我在 AngularJS 中遇到了一个奇怪的问题,其中 MainCtrl 根本没有触发。我去了localhost/,它重定向到localhost/#/但页面是空白的。控制台中没有错误/消息。我可以确认 /views/main.html 可以公开访问。我不知道为什么这不起作用。我错过了什么吗?

angular.module('TurkApp', ['ngCookies']).config([
  '$routeProvider',
  function ($routeProvider) {
    $routeProvider.when('/', {
      templateUrl: '/views/main.html',
      controller: 'MainCtrl'
    }).otherwise({ redirectTo: '/' });
  }
]);
  angular.module('TurkApp', []).controller('MainCtrl', [
    '$scope',
    '$http',
    '$location',
    '$cookies',
    function ($scope, $http, $location, $cookies) {
      $scope.questionIsLoading = true;
      $scope.answerButtonsDisabled = true;
      $scope.showHelp = false;
      $scope.currentRetries = 0;
      $scope.acceptedHit;
      $scope.currentQuestionText = null;
      $scope.currentQuestionID = null;
      var AssignmentID, Interest;
      var getInterest = function () {
        return $cookies.interest;
      };
      var getAssignmentID = function () {
        var qsRegex = new RegExp('(?:\\?|&)AssignmentID=(.*?)(?=&|$)', 'gi'), m, assignmentID = false;
        while ((match = qsRegex.exec(document.location.search)) != null) {
          assignmentID = match[1];
        }
        if (!assignmentID) {
          assignmentID = $location.search()['AssignmentID'];
        }
        $scope.acceptedHit = assignmentID == 'ASSIGNMENT_ID_NOT_AVAILABLE' || !assignmentID ? false : true;
        return assignmentID;
      };
      $scope.loadNextQuestion = function () {
        $scope.questionIsLoading = $scope.answerButtonsDisabled = true;
        $http.get('/workers/' + Interest + '/next-question').success(function (data, status) {
          $scope.currentQuestionText = data.text;
          $scope.currentQuestionID = data.id;
          $scope.questionIsLoading = $scope.answerButtonsDisabled = false;
        }).error(function () {
          console.log('Answer send failed');
        });
      };
      $scope.sendAnswer = function (answer) {
        if (!$scope.questionIsLoading && !$scope.answerButtonsDisabled) {
          $scope.questionIsLoading = $scope.answerButtonsDisabled = true;
          $http.post('/workers/' + Interest + '/answer-question', {
            question_id: $scope.currentQuestionID,
            question_text: $scope.currentQuestionText,
            answer: answer
          }).success(function (data, status) {
            $scope.loadNextQuestion();
          }).error(function () {
            console.log('Answer send failed');
          });
        }
      };
      $scope.toggleHelp = function () {
        $scope.showHelp = $scope.showHelp ? false : true;
      };
      var init = function () {
        AssignmentID = getAssignmentID();
        Interest = getInterest();
        $scope.loadNextQuestion();
      };
      init();
    }
  ]);
4

1 回答 1

4

您正在创建模块“TurkApp”两次,从而丢失了使用第一个模块注册的配置:

angular.module('TurkApp', ['ngCookies'])

当您在 angular.module 函数中包含第二个参数时,它会创建模块。如果省略第二个参数,则假定模块存在并“扩展”它。

改变:

angular.module('TurkApp', [])

到:

angular.module('TurkApp')

请参阅此处的用法部分 - http://docs.angularjs.org/api/angular.module

于 2013-09-11T21:11:15.210 回答