javascript - AngularJS app architecture/structure -
i developing application following structure:
|--------------------- header -------------------| | | |-----sidebar-----|-----------main view----------| | | | | constraints | list of topics | | | | | | | | | | |_________________________________________________
constraint selections made in sidebar effect list of topics shown in main view area.
now works fine have come stage app becoming more complex.
in cases want replace in main view , not show list of topics show topic (render template e.g views/topic.html) still want keep sidebar , not reload it. , able return list of topics.
this new topic view within main view need use controller as-well controller sidebar 'resultscontroller'.
currently use ng-route , following structure:
$routeprovider.when('/', { templateurl: 'views/results.html' }); <ng-view></ng-view> // views/results.html //------------------------------------------------------ <div data-ng-controller="resultscontroller"> <div ng-include src="'views/header.html'"></div> <div ng-include src="'views/sidebar.html'"></div> <div id="main view"> <div ng-repeat="topic in topics"></div> </div> </div>
what correct way structure/architect app?
(i have looked ui-router way go? if how structure routes/view/controllers?)
ui-router can handle in 2 ways, nested views , named views:
nested views:
http://plnkr.co/edit/ngpvmjsxracmlyx3wba5?p=preview
in example, views nest, starting top, side nested in top, list , item nested in side.
$stateprovider.state('top', { url: "", templateurl: "header.html", controller: 'topctrl' }) .state('top.side', { url: '/app', templateurl: "side.html", controller: 'filterctrl' }) .state('top.side.list', { url: "/items?query", templateurl: "items.html", controller: 'listctrl' }) .state('top.side.item', { url: "/:id", templateurl: "item.html", controller: 'itemctrl' });
named views:
another way define named placeholders views, plug them in view definitions on state states. these called "multiple named views". http://plnkr.co/edit/lz00gaxowmgtaba3dc8a?p=preview
the placeholders named ui-views:
<body ng-app="nested"> <div id="header" ui-view="header"></div> <div id="side" ui-view="side"></div> <div id="content" ui-view="content"></div> </body>
those placeholders filled in views state definitions:
.state('top', { url: "", views: { header: { templateurl: "header.html" }, } }) .state('top.list', { url: "/items?query", views: { "side@": { templateurl: "side.html", controller: 'filterctrl' }, "content@": { templateurl: "items.html", controller: 'listctrl' } } }) .state('top.item', { url: "/:id", views: { "side@": { templateurl: "side.html", controller: 'filterctrl' }, "content@": { templateurl: "item.html", controller: 'itemctrl' } } });
the "side@" naming convention means plug ui-view named "side" exists @ "" (root/empty string) state. used can define additional named subviews inside other templates, plug named ui-views using "details@top.item".
Comments
Post a Comment