我使用嵌套視圖ui-router
在用戶個人資料頁面上顯示包含用戶數據的幾個表。下面是路由的概述:如何添加用戶控制器時使用ui路由器嵌套視圖?
routes.config.js
(function() {
'use strict';
angular.module('app.routes').config(routesConfig);
routesConfig.$inject = ['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider'];
function routesConfig($stateProvider, $locationProvider, $urlRouterProvider, helper){
// Set the following to true to enable the HTML5 Mode
// You may have to set <base> tag in index and a routing configuration in your server
$locationProvider.html5Mode(false);
// defaults to dashboard
$urlRouterProvider.otherwise('/app/home');
//
// Application Routes
// -----------------------------------
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: helper.basepath('app.html'),
resolve: helper.resolveFor('fastclick', 'modernizr', 'icons', 'screenfull', 'animo', 'sparklines', 'slimscroll', 'classyloader', 'toaster', 'whirl','loaders.css', 'spinkit','jquery-ui', 'jquery-ui-widgets','weather-icons', 'skycons')
})
.state('app.home', {
url: '/home',
title: 'Home',
templateUrl: helper.basepath('home.html'),
})
.state('app.user', {
abstract: true,
templateUrl: helper.basepath('user.html'),
// controller: 'UserPageController',
// controllerAs: 'userCtrl',
resolve: helper.resolveFor('datatables')
})
.state('app.user.dashboard', {
url: '/user',
title: 'User',
controller: 'UserPageController',
controllerAs: 'userCtrl',
views: {
'eventTable': {
templateUrl: helper.basepath('userProfile/eventTable.html'),
controller: 'EventTableController',
controllerAs: 'eventCtrl'
},
'bankStatement': {
templateUrl: helper.basepath('userProfile/bankStatement.html'),
controller: 'BankStatementController',
controllerAs: 'bankCtrl'
},
'purchasesTable': {
templateUrl: helper.basepath('userProfile/purchasesTable.html'),
controller: 'PurchasesTableController',
controllerAs: 'purchasesCtrl'
},
'paymentsTable': {
templateUrl: helper.basepath('userProfile/paymentsTable.html'),
controller: 'PaymentsTableController',
controllerAs: 'paymentsCtrl'
},
'clicksTable': {
templateUrl: helper.basepath('userProfile/clicksTable.html'),
controller: 'ClicksTableController',
controllerAs: 'clicksCtrl'
},
}
});
} // routesConfig
})();
當我嘗試添加另一個控制器來顯示用戶的基本信息(我已經嘗試添加控制器app.user
和app.user.dashboard
)的控制器從未激活。下面是相關代碼的其餘部分:
user.html
<h3 class="fixedSubHeader">{{userCtrl.userInfo}}</h3>
<section ui-view="eventTable"></section>
<section ui-view="bankStatement"></section>
<section ui-view="paymentsTable"></section>
<section ui-view="purchasesTable"></section>
<section ui-view="clicksTable"></section>
userPage.controller.js
(function() {
'use strict';
angular
.module('app.userPage')
.controller('UserPageController', UserPageController);
UserPageController.$inject = ['$resource'];
function UserPageController($resource) {
var vm = this;
activate();
// console.log("USER!!!!!");
////////////////
function activate() {
// Ajax
$resource('server/basic-info-table.json').query().$promise.then(function(userInfo) {
vm.userInfo = userInfo;
});
}
}
})();
我本來以爲這是與userPage控制器有問題,所以我嘗試注入$scope
並擺脫var vm = this
語法。然而,這種方法也沒有奏效。任何幫助將不勝感激。謝謝