我怎样才能让$ routeProvider在angularjs

问题描述:

我试图手动构建一个shell试图弄清楚它是如何工作

结构正常工作:

- application(php stuff) 

- webroot 
-- app 
--- app.js 

-- templates 
--- main 
---- login 
----- login.html 

index.html (with angularjs libs) 

app.js:

function config($routeProvider){ 
    $routeProvider 
     .when("/login",{ 
      templateUrl: "../templates/main/login/login.html" 
     }) 
     .otherwise({ redirectTo:"/other" }); 
} 


angular 
    .module("app", ["ngRoute", "ngResource"]) 
    .config(config); 

登录:

<div>LOGIN test template</div> 

“/ other”不会退出。 此外,我尝试了不同的pathes为: templateUrl: “模板/主/登录/ login.html的” templateUrl: “根目录/模板/主/登录/ login.html的”

由URL带有一个方式“#”(如nameDomain.com/#/login,但它应该是nameDomain.com/#login),但似乎angularjs只允许/ nameUrlTemplate

看看角的文档为routeProvider,因为这应该清理一些你的困惑。

我想你想在这里做的是使用你的.otherwise现有的路线是什么,因为自从/other没有在任何地方定义,角不知道你是什么吧:

var app = angular.module('app', ['ngRoute', 'ngResource']); 
app.config(function config($routeProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl: '../templates/main/index.html' 
    }) 
    .when('/login', { 
     templateUrl: '../templates/main/login/login.html' 
    }) 
    .otherwise({ 
     redirectTo: '/' 
    }); 
}); 

我相信这应该会给你你正在寻找的效果。显然你可以在你认为合适的时候调整它,例如,如果你想为任何你没有定义的东西拥有404路由/模板。就目前而言,这将永远回落到不存在的特定路线的主页。当你考虑用户和登录/注销状态时,这会变得更加复杂,但这更多的是一个概念验证。

此外,ngRoute将默认渲染的,因为这/#/foo格式的URL使用hashbang风格的格式,从而使爬虫的搜索引擎能够解析和读取一个JavaScript webpp的各个页面/状态。你指的是一个文档片段,它们是而非可以通过抓取工具进行索引。您可以详细了解Google的Webmaster docs上的差异。

+0

这有什么错我的WHEN。当( '/登录',{ templateUrl: '../templates/main/login/login.html' }) – Donovant 2015-02-06 10:12:11

+0

另外:我想有/#改为登录/#/登录 – Donovant 2015-02-06 10:44:42