I am using the AngularUI Router framework for the first time and wondered how I could improve the below code.
Basically this all works fine but eventually (in the project I am working on) there will be 20 questions or more and I dont want to state the same 'templateUrl' & 'controller' for every state.
The below is a slimmed down version of what I mean:
index.html
<div ui-view></div>
questions.html
<a ui-sref="q1">q1</a>
<a ui-sref="q2">q2</a>
<div ng-show="$state.current.name === 'q1'">q1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere.</div>
<div ng-show="$state.current.name === 'q2'">q2. Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque felis.</div>
app.js
angular.module('foo', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('q1', {
url: '/',
templateUrl: 'questions.html',
controller: 'questionsCtrl'
})
.state('q2', {
url: '/q2',
templateUrl: 'questions.html',
controller: 'questionsCtrl'
});
})
.controller('questionsCtrl', function($scope, $state) {
$scope.$state = $state;
});
;
I would like to follow best practice and keep the app.js as small/manageable as possible.
Cheers