出于某种原因,我的angularjs路由无法正常工作。单击链接时,视图不会呈现。我在控制台中看不到任何错误。
这是我的设置
(function(){
var app = angular.module('myapp',[
'ngRoute',
//'quizcontrollers'
]);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/questions', {
controller: 'quizcontroller',
templateUrl: 'questions.html'
})
// .when('/quiz/', {
// templateUrl: 'partials/phone-detail.html',
// controller: 'PhoneDetailCtrl'
// }).
.otherwise({
redirectTo: '/'
});
}]);
}());
quiz.js
(function(){
var app = angular.module("myapp",[]);
app.controller ("quizcontroller",function($scope,$http,$location,$window){
$scope.message ="hello"
})
}())
附加quiz.php
<html ng-app="myapp">
<head></head>
<body>
<a href="#/questions">Test</a>
<div data-ng-view=""></div>
<script src="/path/backend/web/assets/js/angular/angular.js"> </script>
<script src="/path/backend/web/assets/js/angular/angular-route.js"> </script>
<script src="/path/backend/web/assets/js/angular/app.js"></script>
<script src="/path/backend/web/assets/js/angular/app-config.js"></script>
<script src="/teachsity/backend/web/assets/js/angular/filters/range.js"></script>
<script src="/path/backend/web/assets/js/angular/controllers/quiz.js"></script>
</body>
</html>
question.html文件与app.js位于同一目录中。
答案 0 :(得分:2)
你声明模块两次从控制器声明中删除。
<html ng-app="myapp">
<head></head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.js"></script>
<a href="#/questions">Test</a>
<div ng-view></div>
</body>
</html>
<script type="text/javascript">
(function()
{
var app = angular.module('myapp',['ngRoute']);
app.config(['$routeProvider',
function($routeProvider)
{
$routeProvider
.when('/',
{
templateUrl : 'questions.html'
})
.when('/questions',
{
controller: 'quizcontroller',
templateUrl: 'questions.html'
})
.otherwise(
{
redirectTo: '/'
});
}]);
app.controller ("quizcontroller",function($scope,$http,$location,$window)
{
$scope.message ="hello"
})
}())
</script>
答案 1 :(得分:0)
这背后的原因是因为$routeProvider
假设questions.html
与您的add-quiz.php
具有相同的路径。所以你必须
从
$routeProvider.
when('/questions', {
controller: 'quizcontroller',
templateUrl: 'questions.html'
})
要
$routeProvider.
when('/questions', {
controller: 'quizcontroller',
templateUrl: '/path/backend/web/assets/js/angular/questions.html'
})