You indeed can be more DRY than that. I would:
Remove all
go()
functions from the views' controllers.Create a new controller (e.g.
NavCtrl
) for the navigation "bar".<form ng-controller="NavCtrl">
Remove the
ngClick
directives from the<option>
elements (since they don't seem to have any effect - at least in Chrome).Add an
ngModel
to the<select>
element to keep track of the selected page/view.Add an
ngChange
listener to the<select>
element to trigger a "redirection" every time the selected page/view changes.<select id="naver" name="naver" ng-model="currentPage" ng-change="go(currentPage)"> <option value="home">Home</option> <option value="first">First</option> <option value="second">Second</option> </select>
Define the
go()
function inside the aforementionedNavCtrl
controller.app.controller('NavCtrl', function NavCtrl($location, $scope) { $scope.currentPage = 'home'; $scope.go = function go(page) { $location.path('/' + page); }; });
See, also, this short demo.