我有两个HTML页面。一个是exam.html,另一个是result.html。在exam.html控制器ExamCtrl中的某些情况下,我使用result.html路由到$location.path。但是,在result页面上,虽然我已在配置文件中添加了控制器ResultCtrl,但它似乎并未加载。

角度配置文件:

angular.module('exam').config(['$stateProvider',
  function ($stateProvider) {
    // Exam state routing
    $stateProvider
      .state('exam', {
        abstract: true,
        url: '/exam',
        template: '<ui-view/>'
      })
      .state('exam.list', {
        url: '',
        templateUrl: 'modules/exam/client/views/exam.client.view.html',
        controller: 'ExamCtrl'
      })
      .state('/result', {
        url: '/result',
        templateUrl: 'modules/exam/client/views/exam-result.client.view.html',
        contoller: 'ResultCtrl'
      });
  }
]);


ExamCtrl:

angular
.module('exam')
.controller('ExamCtrl', ['$scope', '$stateParams', '$location', 'Authentication', '$http',
  function ($scope, $stateParams, $location, Authentication, $http) {


$scope.submitAnswer = function (selected) {
      //some code
      if (qtnCounter > 5) {
        // loads the result page.
        $location.path('/result');
      } else {
        $scope.getQues();
      }
    };
  }
]);


结果Ctrl:

angular
.module('exam')
.controller('ResultCtrl', ['$scope', '$stateParams', '$location', 'Authentication', '$http',
  function ($scope, $stateParams, $location, Authentication, $http) {

    console.log('ResultCtrl');

   }
]);


result.html:

<body ng-controller="ResultCtrl">
    <h1>Result page!</h1>
</body>

最佳答案

不用$location而是使用$state更改应用程序中的给定状态。
在控制器中注入$state,然后调用transitionTo(),这将加载视图并设置控制器

angular
.module('exam')
.controller('ExamCtrl', ['$scope', '$state', 'Authentication', '$http',
  function ($scope, $state, Authentication, $http) {


$scope.submitAnswer = function (selected) {
      //some code
      if (qtnCounter > 5) {
        // loads the result page.
        $state.transitionTo('/result');
      } else {
        $scope.getQues();
      }
    };
  }
]);

10-07 19:58
查看更多