30 angularjs angular-ui-router
我很难找到任何关于通过数据库动态使用ui-router的文档.对于课程而言,一切都是硬编码的.
我的Json:
[
{
"name": "root",
"url": "/",
"parent": "",
"abstract": true,
"views": [
{"name": "header", "templateUrl": "/app/views/header.html"},
{"name" :"footer", "templateUrl": "/app/views/footer.html" }
]
},
{
"name": "home",
"url": "",
"abstract" : false,
"parent": "root",
"views": [
{"name": "container@", "templateUrl": "/app/views/content1.html"},
{"name" :"left@", "templateUrl": "/app/views/left1.html" }
]
},
{
"name": "about",
"url": "/about",
"abstract": false,
"parent": "root",
"views": [
{"name": "container@", "templateUrl": "/app/views/content2.html"},
{"name" :"left@", "templateUrl": "/app/views/left2.html" }
]
}
]
Run Code Online (Sandbox Code Playgroud)
我的应用:
'use strict';
var $stateProviderRef = null;
var $urlRouterProviderRef = null;
var app = angular.module('app', ['ngRoute', 'ui.router']);
app.factory('menuItems', function ($http) {
return {
all: function () {
return $http({
url: '/app/jsonData/efstates.js',
method: 'GET'
});
}
};
});
app.config(function ($locationProvider, $urlRouterProvider, $stateProvider) {
$urlRouterProviderRef = $urlRouterProvider;
$stateProviderRef = $stateProvider;
$locationProvider.html5Mode(false);
$urlRouterProviderRef.otherwise("/");
});
app.run(['$q', '$rootScope', '$state', 'menuItems',
function ($q, $rootScope, $state, menuItems) {
menuItems.all().success(function (data) {
angular.forEach(data, function (value, key) {
$stateProviderRef.state(name = value.name, {
"url": value.url,
"parent" : value.parent,
"abstract": value.abstract,
"views": {
// do not want the below hard coded, I want to loop
// through the respective json array objects and populate state & views
// I can do this with everything but views.
// first loop
'header': { 'templateUrl': '/app/views/header.html' },
'footer': { 'templateUrl': '/app/views/footer.html' },
// second loop
'left@': { 'templateUrl': '/app/views/left1.html' },
'container@': { 'templateUrl': '/app/views/container1.html' },
// third loop
'left@': { 'templateUrl': '/app/views/left2.html' },
'container@': { 'templateUrl': '/app/views/container2.html' },
}
});
});
$state.go("home");
});
}]);
Run Code Online (Sandbox Code Playgroud)
我正在动态地配置我的视图.有任何想法吗?
更新:
对于任何有兴趣的人,我为每个RadimKöhler做了一个Plunker.我很感激帮助.
我认为ui-router是用于角度的事实路由器,并且通过动态,它将使大型应用程序更容易管理.
Rad*_*ler 24
有一个plunker显示我们如何动态配置视图.更新版本.run()将是这样的:
app.run(['$q', '$rootScope', '$state', '$http',
function ($q, $rootScope, $state, $http)
{
$http.get("myJson.json")
.success(function(data)
{
angular.forEach(data, function (value, key)
{
var state = {
"url": value.url,
"parent" : value.parent,
"abstract": value.abstract,
"views": {}
};
// here we configure the views
angular.forEach(value.views, function (view)
{
state.views[view.name] = {
templateUrl : view.templateUrl,
};
});
$stateProviderRef.state(value.name, state);
});
$state.go("home");
});
}]);
Run Code Online (Sandbox Code Playgroud)
检查所有的行动在这里
Rad*_*ler 19
我必须添加一个改进的版本,甚至可以做更多的版本.
所以,现在我们还是会负荷状态动态-使用$http,json并在定义状态.run()
但现在我们可以使用url导航到任何动态状态(只需将其放在地址栏中).
魔术内置于UI-Router中 - 请参阅doc的这一部分:
该
deferIntercept(defer)禁用(或启用)延迟位置更改拦截.
如果您希望自定义同步URL的行为(例如,如果您希望延迟转换但保留当前URL),请在配置时调用此方法.然后,在运行时,
$urlRouter.listen()在配置了自己的$ locationChangeSuccess事件处理程序之后调用.
引用片段:
var app = angular.module('app', ['ui.router.router']);
app.config(function($urlRouterProvider) {
// Prevent $urlRouter from automatically intercepting URL changes;
// this allows you to configure custom behavior in between
// location changes and route synchronization:
$urlRouterProvider.deferIntercept();
}).run(function($rootScope, $urlRouter, UserService) {
$rootScope.$on('$locationChangeSuccess', function(e) {
// UserService is an example service for managing user state
if (UserService.isLoggedIn()) return;
// Prevent $urlRouter's default handler from firing
e.preventDefault();
UserService.handleLogin().then(function() {
// Once the user has logged in, sync the current URL
// to the router:
$urlRouter.sync();
});
});
// Configures $urlRouter's listener *after* your custom listener
$urlRouter.listen();
});
Run Code Online (Sandbox Code Playgroud)
所以更新的plunker就在这里.我们现在可以使用甚至.otherwise()导航到最近定义的状态,或者通过url去那里:
该.config()阶段
app.config(function ($locationProvider, $urlRouterProvider, $stateProvider) {
// Prevent $urlRouter from automatically intercepting URL changes;
// this allows you to configure custom behavior in between
// location changes and route synchronization:
$urlRouterProvider.deferIntercept();
$urlRouterProvider.otherwise('/other');
$locationProvider.html5Mode({enabled: false});
$stateProviderRef = $stateProvider;
});
Run Code Online (Sandbox Code Playgroud)
该.run()阶段
app.run(['$q', '$rootScope','$http', '$urlRouter',
function ($q, $rootScope, $http, $urlRouter)
{
$http
.get("myJson.json")
.success(function(data)
{
angular.forEach(data, function (value, key)
{
var state = {
"url": value.url,
"parent" : value.parent,
"abstract": value.abstract,
"views": {}
};
angular.forEach(value.views, function (view)
{
state.views[view.name] = {
templateUrl : view.templateUrl,
};
});
$stateProviderRef.state(value.name, state);
});
// Configures $urlRouter's listener *after* your custom listener
$urlRouter.sync();
$urlRouter.listen();
});
}]);
Run Code Online (Sandbox Code Playgroud)
在这里检查更新的plunker
| 归档时间: |
|
| 查看次数: |
35544 次 |
| 最近记录: |