ngRoute 不支持 巢狀 View 及多重 View 的實現。
幸好在 Google 上找到了救兵 ui-router,以下是兩種寫法的差別。
ngRoute 寫法:
app.js config:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config(['$routeProvider', function($routeProvider) { | |
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'}); | |
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'}); | |
$routeProvider.otherwise({redirectTo: '/view1'}); | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="menu"> | |
<li><a href="#/view1">view1</a></li> | |
<li><a href="#/view2">view2</a></li> | |
</ul> | |
<div ng-view></div> |
app.js config:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) { | |
// | |
// For any unmatched url, redirect to | |
$urlRouterProvider.otherwise("/main"); | |
// | |
// Now set up the states | |
$stateProvider | |
.state('main', { | |
url: "/main", | |
templateUrl: "partials/main.html" | |
}) | |
.state('main.view1', { | |
url: "/view1", | |
templateUrl: "partials/partial1.html", | |
controller: "MyCtrl1" | |
}) | |
.state('main.view2', { | |
url: "/view2", | |
templateUrl: "partials/partial2.html", | |
controller: "MyCtrl2" | |
}); | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div ui-view></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="menu"> | |
<li><a ui-sref="main.view1">view1</a></li> | |
<li><a ui-sref="main.view2">view2</a></li> | |
</ul> | |
<div ui-view></div> | |
<div>Angular seed app: v<span app-version></span></div> |
Reference
GitHub - angular-ui/ui-router