N13*_*N13 14 routes angularjs angular-ui-router ngroute
我想开始使用Angular的ui-router而不是ngRoute.最初,我的app配置看起来像
myApp.config(["$routeProvider",
function($routeProvider) {
$routeProvider
.when("/search", {
templateUrl: "partials/customerSearch.html"
})
.when("/home", {
templateUrl: "partials/home.html"
})
.when("/login", {
templateUrl: "partials/login.html",
controller: "LoginCtrl"
})
.otherwise({
redirectTo: "/home"
})
;
}
]);
Run Code Online (Sandbox Code Playgroud)
我换掉了库,并更改了配置.我知道我仍然可以使用$routeProvider
,但这似乎是一种传统的解决方法.
myApp.config(["$urlRouterProvider", "$stateProvider",
function($urlRouterProvider, $stateProvider) {
$urlRouterProvider
.when("/search", "partials/customerSearch.html")
.when("/home", "partials/home.html")
.when("/login", "partials/login.html")
.otherwise("/home")
;
$stateProvider
.state({
name: "customer",
url: "/customer/:username",
templateUrl: "partials/customer.html"
})
.state({
parent: "customer",
name: "details",
url: "/details",
templateUrl: "partials/customerDetails.html"
})
;
}
]);
Run Code Online (Sandbox Code Playgroud)
这给了我似乎表明$digest
陷入循环的错误.我怀疑这条.otherwise("/home")
规则.我handler
是否正确指定了s,就好像它们是模板URL一样?
如果我评论出.when()
s,除了之外没有任何作用"/customer/:username"
.我是否必须为每条路线定义一个州?如果是这样,那么两者兼而有之$urlRouterProvider
又是$stateProvider
什么意思?问的不同,每个人应该做什么?
che*_*ard 13
这是一个基本的例子,我前一段时间,在ui-router配置中使用名称间隔控制器,以及一个嵌套路由(第二个选项卡):http://plnkr.co/edit/2DuSin?p = preview
template:
可以更改为templateUrl:
指向HTML文件.
var app = angular.module('plunker', ['ui.bootstrap', 'ui.bootstrap.tpls','ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('state1', {
url: "/",
template: 'Hello from the first Tab!',
controller: 'FirstCtrl',
data:{}
})
.state('state2', {
url: "/route2",
template: 'Hello from the 2nd Tab!<br>' +
'<a ui-sref="state2.list">Show List</a><div ui-view></div>',
controller: 'SecondCtrl',
data: {}
})
.state('state2.list', {
url: '/list',
template: '<h2>Nest list state</h2><ul><li ng-repeat="thing in things">{{thing}}</li></ul>',
controller: 'SecondCtrl',
data: {}
});
});
Run Code Online (Sandbox Code Playgroud)
控制器:
app.controller('FirstCtrl', ['$scope', '$stateParams', '$state', function($scope,$stateParams,$state){
}]);
app.controller('SecondCtrl', ['$scope', '$stateParams', '$state', function($scope, $stateParams, $state){
$scope.things = ["A", "Set", "Of", "Things"];
}]);
Run Code Online (Sandbox Code Playgroud)