[英]Architecture for multi-page angular application
我想我会建议只使用多个命名视图。 每个命名视图都可以拥有自己的控制器:
$stateProvider
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'templates/app.tpl.html',
},
'section1': {
controller: 'Section1Controller as vm',
templateUrl: 'templates/section1.tpl.html'
},
'section2': {
controller: 'Section2Controller as vm',
templateUrl: 'templates/section2.tpl.html'
},
'section3': {
controller: 'Section3Controller as vm',
templateUrl: 'templates/section3.tpl.html'
},
'section4': {
controller: 'Section4Controller as vm',
templateUrl: 'templates/section4.tpl.html'
}
}
})
.state('page2', {
url: '/page2',
views: {
'': {
templateUrl: 'templates/page2.tpl.html',
},
'section1': {
controller: 'Section1Controller as vm',
templateUrl: 'templates/section1.tpl.html'
},
'section2': {
controller: 'Section2Controller as vm',
templateUrl: 'templates/section2.tpl.html'
},
'section3': {
controller: 'Section3Controller as vm',
templateUrl: 'templates/section3.tpl.html'
}
}
})
然后,当您布置视图时,它们看起来像这样:
<div ui-view="section1"></div>
<div ui-view="section2"></div>
<div ui-view="section3"></div>
<div ui-view="section4"></div>
我将使用指令允许多个控制器,重用第1页和第2页之间的代码,并准备迁移到Angular 2。
您的页面看起来像:
<section1></section1>
<section2></section2>
<section3></section3>
<section4></section4>
你必须为每个部分写一个指令:
module.directive('section1', function() {
return {
scope: {},
bindToController: {
},
controller: function() { },
controllerAs: 'vm',
template: `
<div>This is section1
</div>
`
}
});
这是一篇关于Angular 1.x模块的文章
如果您对使用TypeScript感兴趣,这里的教程包含两个页面,其中2个共享部分使用指令 ,如上所述。 查看接近结尾的部分“使用共享指令的示例页面”。 该教程包含一个github存储库 。 在该教程中,page1看起来像
h1 page1
page1-section1
page1-section2
并且,第二页共享相同的部分:
h1 page2
page2-section2
page2-section1
page1和page2之间的控制器非常相似,并使用相同/共享指令(DigiSection1.Section1Directive)创建节标签:
angular
.module('digiangularjs.page1', [])
.controller('agendaController', Page1Controller)
.directive("page1Section1", [() => new DigiSection1.Section1Directive()])
.directive("page1Section2", [() => new DigiSection2.Section2Directive()])
;
对于第二页,我们使用相同的指令,但是
angular
.module('digiangularjs.page2', [])
.controller('page2Controller', Page2Controller)
.directive("page2Section1", [() => new DigiSection1.Section1Directive()])
.directive("page2Section2", [() => new DigiSection2.Section2Directive()])
;
关于Mike的回答我将路由级模板定义为高级布局容器的单个组件。
.state('page1', {
url: '/page1',
template: '<page1></page1>'
})
.state('page2', {
url: '/page2',
template: '<page2></page2>
});
然后在你的<page>
组件(它只是规定嵌套指令/组件的布局)中你可以这样做:
.component('page1', {
template: [
'<section1></section1>',
'<section2></section2>',
'<section3></section3>'
].join('')
});
此外,我意识到你写了“多页面应用程序”,这表明你根本不打算使用路由器。 如果是这种情况,您的后端将不得不处理动态布局生成,这是一个完全不同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.