angularjs - Multiple module nested ui-router with multiple ui-views -
i'm working on complex ui architecture based on angular , angular ui router. i'm trying define routes nested ui-views on multiple modules.
here i'm trying do: http://plnkr.co/edit/sxjopqagyezfcyfuregr?p=preview
files here:
index.html
<body ng-app="app"> <h1 ui-view="title"></h1> <div ui-view="sidebar1"></div> <ul ui-view="sidebar2"></ul> <div ui-view="content"></div> </body>
app.js
angular .module('app', ['ui.router', 'page1']) .config(['$locationprovider','$stateprovider', '$urlrouterprovider', function ($locationprovider, $stateprovider, $urlrouterprovider) { $locationprovider.html5mode(true); $stateprovider.state({ name: "page1", url: "/page1", abstract: true }); $urlrouterprovider.otherwise('/page1'); }]);
page1.js
angular .module('page1', ['ui.router']) .config(function ($stateprovider) { $stateprovider.state({ name: 'page1.main', views: { title: { template: "my title" }, sidebar1: { template: "<ul><li ng-repeat='item on items'>{{item}}</li></ul>", controller: function($scope) { $scope.items = ['foo1', 'bar1']; } }, sidebar2: { template: "<ul><li ng-repeat='item on items'></li></ul>", controller: function($scope) { $scope.items = ['foo2', 'bar2']; } }, content: { template: "<div ng-repeat='one in many'>{{one}}</div>", resolve: { stuff: function () { return [1,2,3,4,5] } }, controller: function($scope, stuff) { $scope.many = stuff; } } } }); });
what missing?
thanks lot in advance.
i've made few changes, , make running here.
firstly, defined in doc: abstract states, child state of abstract state must defined, i.e. must have url defined (which ui-router
find out state use... abstract parent unreachable)
$stateprovider.state({ name: 'page1.main', url : '/main',
and default route changed to:
$urlrouterprovider.otherwise('/page1/main');
and important thing use correct ui-view
naming:
$stateprovider.state({ name: 'page1.main', url : '/main', views: { "title@": { template: "my title" }, "sidebar1@": { template: "<ul><li ng-repeat='item on items'>{{item}}</li></ul>", controller: function($scope) { $scope.items = ['foo1', 'bar1']; } }, "sidebar2@": { template: "<ul><li ng-repeat='item in items'>{{item}}</li></ul>", controller: function($scope) { $scope.items = ['foo2', 'bar2']; } }, "content@": { ...
what can see, view names suffixed @
, means: search them in top root, unnamed view (usually index.html)
see more here: view names - relative vs. absolute names
a small extract doc:
/////////////////////////////////////////////////////// // absolute targeting using '@' // // targets view within state or ancestor // /////////////////////////////////////////////////////// // absolutely targets 'info' view in state, 'contacts.detail'. // <div ui-view='info'/> within contacts.detail.html "info@contacts.detail" : { } // absolutely targets 'detail' view in 'contacts' state. // <div ui-view='detail'/> within contacts.html "detail@contacts" : { } // absolutely targets unnamed view in parent 'contacts' state. // <div ui-view/> within contacts.html "@contacts" : { }
the working plunker.
edit: how preserve /page1
in url
as (almost) ui-router
, can give behaviour. trick is, reset url evaluation start on root level - child (without parent url part). , done magical "^
" sign
$stateprovider.state({ name: 'page1.main', url : '^/page1', views: { ....
here doc:
- absolute routes (^)
(a cite)
if want have absolute url matching, need prefix url string special symbol '^'.
Comments
Post a Comment