我是angular.js的新手,很难理解routeProvider。在以下代码中:
.when('/notes', {
templateUrl: 'templates/pages/notes/index.html'
})第一个(‘/note’,{...});代表什么?附件是完整的代码片段:
angular.module('NoteWrangler', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/notes', {
templateUrl: 'templates/pages/notes/index.html'
})
.when('/users', {
templateUrl: 'templates/pages/users/index.html'
})
.when('/notes/new', {
templateUrl: 'templates/pages/notes/edit.html'
})
.otherwise({
redirectTo: '/notes'
});
}]);发布于 2015-10-09 22:24:39
当访问mydomain.com/#/notes链接时,将获取并显示模板mydomain.com/templates/pages/notes/index.html。
这有意义吗?
发布于 2016-04-01 23:22:31
当域名后的url中有#/注释时,它将重定向到在templateUrl中为该路由指定的模板。在上面的例子中: url : domain/#/notes模板: index.html
发布于 2017-08-02 23:12:00
.when('/notes', {
templateUrl: 'templates/pages/notes/index.html'
})这意味着
当客户端(浏览器)请求路由"/notes“时,例如http://localhost:8080/notes或http://yourwebsite/notes
angular加载位于路径‘/index.html//index.html’中的HTML页面。
在您的应用程序中,您需要具有该文件夹结构
'/users'和'/notes/new'也是如此
否则就是一个包罗万象的东西
.otherwise({
redirectTo: '/notes'
});您没有指定的任何其他路由都由它处理,因此在您的代码中,您强制重定向到/notes (我假设它是您的主页)。
其他示例:您还可以使用
.when('/notes', {
template: '<div>Hello world!</div>'
})它将直接为您提供HTML,而不是模板HTML页面
TL DR;当遇到此路由(
'/notes')时,我将给您此templateUrlHTML页面
https://stackoverflow.com/questions/33040669
复制相似问题