[英]$urlRouterProvider is causing an error, despite ui-router being injected - angularjs
[英]UI-router nested view not being injected
我有一個帶有ui-view標記的根index.html
文件,一個主模板文件home.html
被注入其中。
home.html
具有ui-view
標記,需要根據當前路線進行更改。 我遵循了官方的ui-router文檔,並編寫了app.js
,但是home.html
標記中的嵌套ui-view
沒有注入視圖。
結構如下:
index.html
| home.html
- dashboard.html
- tables.html
index.html
有一個顯示home.html
的ui-view標簽。 home.html
是帶有ui-view
標簽的模板,根據路由,該模板需要顯示dashboard.html
或tables.html
。
資源:
index.html-
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="lib/css/main.min.css"/>
<script src="lib/js/main.min.js"></script>
<script type="text/javascript" src="js/dashboard.min.js"></script>
</head>
<body ng-controller="MasterCtrl">
<div ui-view></div>
</body>
</html>
templates / home.html-
<div id="page-wrapper" ng-class="{'open': toggle}" ng-cloak>
<!-- Sidebar -->
<div ng-include="sidebar.html" scope="" onload=""></div>
<!-- End Sidebar -->
<div id="content-wrapper">
<div class="page-content">
<!-- Header Bar -->
<div ng-include="header.html" scope="" onload=""></div>
<!-- End Header Bar -->
<!-- Main Content -->
<div ui-view></div>
</div><!-- End Page Content -->
</div><!-- End Content Wrapper -->
module.js
angular.module('app', ['ui.bootstrap', 'ui.router', 'ngCookies']);
routes.js
'use strict';
angular.module('app').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For unmatched routes
$urlRouterProvider.otherwise('/');
// Application routes
$stateProvider
.state('home', {
abstract: true,
url: '/',
templateUrl: 'templates/home.html'
})
.state('home.dashboard', {
url: '/dashboard',
templateUrl: 'templates/dashboard.html'
})
.state('home.tables', {
url: '/tables',
templateUrl: 'templates/tables.html'
});
}
]);
在將它們添加為路由而不從home
繼承的情況下,通常會將它們注入index.html
,這意味着路由本身不是問題。
如何根據路線繼承模板並注入視圖?
您需要將ui-router作為依賴項添加到模塊
angular.module('app',['ui.router'])
還請確保您已添加參考
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<!-- UI-Router -->
<script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>
解決了我的問題。 我從抽象狀態home
刪除了url
標記,只是將home.dashboard
設置為/
。
這有效:
'use strict';
angular.module('app').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For unmatched routes
$urlRouterProvider.otherwise('/');
// Application routes
$stateProvider
.state('home', {
abstract: true,
templateUrl: 'templates/home.html'
})
.state('home.dashboard', {
url: '/',
templateUrl: 'templates/dashboard.html'
})
.state('home.tables', {
url: '/tables',
templateUrl: 'templates/tables.html'
});
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.