【发布时间】:2017-07-20 00:07:38
【问题描述】:
我是 Angular JS 的新手,遇到了一个问题……我不知道发生了什么。请帮助解决它。
我有索引文件并且 app.js 已连接,app 正在使用 ngRoute...见下面的代码。
var app = angular.module('routedapp',['ngRoute']);
//app.config("$routeProvider",function($routeProvider)) also tried this
app.config(function($routeProvider) {
$routeProvider.when("/",{
templateUrl: "views/home.html"
})
.when("/post",{
templateUrl: "views/post.html"
})
.when("/contact",{
templateUrl: "views/contact.html",
controller: "contactCtrl"
})
})
app.controller('contactCtrl', function($scope){
$scope.lalteen = function(){
console.log("clicked");
}
})
并且有 4 页... index.html 、 home.html 、 post.html 和 contact.html。
//index.html代码
<html>
<head>
<title>Routed</title>
</head>
<body ng-app="routedapp" >
<div>welcome</div>
<div>
<p ng-click="lalteen()">click me</p>
</div>
<!-- views here-->
<div ng-view >
</div>
<script src="vendor/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.js"></script>
<script src="app.js"></script>
</body>
</html>
最初,当 index.html 页面加载时,它会在视图中显示 home.html 内容,home.html 有 3 个指向主页、帖子和联系页面的链接......当我点击任何一个时......它不会改变查看但不断更改网址,例如:
http://localhost/route/#!/#%2F //for home <a> tag
http://localhost/route/#!/#contact //for contact <a> tag and other like that.
PS:以下是我在 post.html、home.html 和 contact.html 中的内容结构,看起来像......
<div>This is post page</div>
<p><a href="#/">Home<a/></p><br>
<p><a href="#/post" ng-click="lalteen()">post</a></p><br>
<p><a href="#/contact">contact</a></p><br>
我是新人...请不要介意我的冗长解释...谢谢
【问题讨论】:
标签: angularjs angular-ui-router ngroute route-provider angularjs-ng-route