TemplateUrl 未显示 home.html
TemplateUrl not displaying the home.html
有人可以帮我解决这个问题吗,它不是为 HomeController 服务的。当我 运行 http://localhost/myangular/
时,它没有在我的 Home.html 中打印文本
index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div ng-view></div>
<!-- Include the AngularJS library -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<!-- Include main.js -->
<script src="js/main.js"></script>
</body>
</html>
main.js
var app = angular.module("myApp", []);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "templates/home.html",
controller: 'HomeController'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('HomeController', function($scope) {
});
home.html
<h1>hello home</h1>
像这样尝试:
app.config(function($routeProvider){
$routeProvider.when('/',{
templateUrl:"/templates/home.html",
controller: 'HomeController'
}).otherwise({ redirectTo: '/'});
});
使用 templateUrl 的相对路径。我只是假设,我不熟悉 angular
我在你的代码中看到的主要问题是你没有加载ngRoute - 因为AngularJS 1.2.0,ngRoute被分离成自己的模块。
解决问题的步骤:
- 添加 ngRoute 库:
//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-route.min.js
- 将其添加为依赖项:
angular.module("myApp",["ngRoute"])
有人可以帮我解决这个问题吗,它不是为 HomeController 服务的。当我 运行 http://localhost/myangular/
时,它没有在我的 Home.html 中打印文本index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div ng-view></div>
<!-- Include the AngularJS library -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<!-- Include main.js -->
<script src="js/main.js"></script>
</body>
</html>
main.js
var app = angular.module("myApp", []);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "templates/home.html",
controller: 'HomeController'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('HomeController', function($scope) {
});
home.html
<h1>hello home</h1>
像这样尝试:
app.config(function($routeProvider){
$routeProvider.when('/',{
templateUrl:"/templates/home.html",
controller: 'HomeController'
}).otherwise({ redirectTo: '/'});
});
使用 templateUrl 的相对路径。我只是假设,我不熟悉 angular
我在你的代码中看到的主要问题是你没有加载ngRoute - 因为AngularJS 1.2.0,ngRoute被分离成自己的模块。
解决问题的步骤:
- 添加 ngRoute 库:
//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-route.min.js
- 将其添加为依赖项:
angular.module("myApp",["ngRoute"])