ng uirouter-directives problem
by Aleksey Filippov
HTML
<link rel="stylesheet" href="https://cdn.jsdelivr.net/foundation/5.5.3/css/foundation.min.css">
<script src="https://cdn.jsdelivr.net/angularjs/1.4.7/angular.min.js"></script>
<script src="https://cdn.jsdelivr.net/angular.ui-router/0.2.15/angular-ui-router.min.js"></script>
<div ui-view=""></div>
<script type="text/ng-template" id="main.html">
<a ui-sref=".list.movies" ui-sref-active="active" class="mtype">movies</a> | <a ui-sref=".list.presentations" ui-sref-active="active" class="mtype">presentations</a>
<hr>
<div ui-view=""></div>
</script>
<script type="text/ng-template" id="years.html">
<ul>
<li ng-repeat="year in listCtrl.years" ui-sref-active="active" ui-sref=".({year: year})" class="year">{{year}}</li>
</ul>
<hr>
<div ui-view=""></div>
</script>
<script type="text/ng-template" id="list.html">
<ol>
<li ng-repeat="i in items">{{i.title}}</li>
</ol>
</script>
CSS
body {padding: 1rem;}
ul {padding:0;margin:0;list-style:none;}
ul li {display: inline-block; margin-right: .5rem; padding: .25rem .45rem;}
.mtype.active { text-decoration: underline; }
.year.active { background-color: #F3C471; color: #fff; }
JavaScript
angular.module('app', ['ui.router'])
.config(routeProviderConfig)
.run(appStart);
appStart.$inject = ['$rootScope', '$state', '$stateParams'];
function appStart($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$state.transitionTo('main');
}
routeProviderConfig.$inject = ['$stateProvider'];
function routeProviderConfig($stateProvider) {
$stateProvider
.state('main', {
url: '',
templateUrl: 'main.html'
})
.state('main.list', {
url: '/contents',
abstract: true,
templateUrl: 'years.html',
controllerAs: 'listCtrl',
controller: ['$state', '$stateParams', function($state, $stateParams) {
var listCtrl = this;
var yearFrom = 1960,
yearTo = 1970;
listCtrl.years = [];
for(var i = yearFrom; i <= yearTo; i++) {
listCtrl.years.push(i);
}
}],
params: {
page: null,
year: null
}
})
.state('main.list.movies', {
url: '/movies?year&page',
templateUrl: 'list.html',
controller: ['$state', '$scope', '$stateParams', function($state, $scope, $stateParams) {
if(!$stateParams.year) {
$stateParams.year = $scope.listCtrl.years[0];
$stateParams.page = 1;
$state.go('.', $stateParams);
}
$scope.header = "Greetings from [movies list] state!";
$scope.items = [
{id: 1, title: 'Cheburashka Begins'},
{id: 2, title: 'The Fury of Cheburashka'},
{id: 3, title: 'Cheburashka. The Legacy'},
{id: 4, title: 'Cheburashka Rises'},
{id: 5, title: 'Cheburashka & Genadiy the Crocodile'}
]
}]
})
.state('main.list.presentations', {
url: '/presentations?year&page',
templateUrl: 'list.html',
controller: ['$state', '$scope', '$stateParams', function($state, $scope, $stateParams) {
...