gpt4 book ai didi

AngularJS - UI 路由器 - 如何配置动态 View

转载 作者:行者123 更新时间:2023-12-02 22:16:12 25 4
gpt4 key购买 nike

我很难找到任何有关通过数据库动态使用 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" }
]
}
]

我的应用程序:

'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");
});
}]);

我很难动态配置我的 View 。有什么想法吗?

<小时/>

更新:

我做了一个Plunker根据 Radim Köhler 对任何感兴趣的人的回答。我很感谢您的帮助。

我认为 ui-router 是 Angular 的事实上的路由器,通过动态它将使大型应用程序更容易管理。

最佳答案

有一个plunker展示我们如何动态配置 View 。 .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");
});
}]);

检查所有操作是否有效 here

关于AngularJS - UI 路由器 - 如何配置动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com