2017-02-05 158 views
2

我有兩個HTML頁面。一個是exam.html,另一個是result.html。在exam.html的控制器ExamCtrl的某些情況下,我使用$location.path路由到result.html。但是,在result頁面上,控制器ResultCtrl似乎無法加載,但我已將它添加到配置文件中。AngularJS用不同的控制器路由到不同的頁面

角配置文件:

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(); 
     } 
    }; 
    } 
]); 

ResultCtrl:

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> 

回答

1

使用$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(); 
     } 
    }; 
    } 
]); 
+0

哦。得到它了。非常感謝! –

+0

Np,開心編碼:) –

相關問題