4

I have a view, which contains a menu and a directive. I would like to have the first item from the menu selected on page load, this then populates the contents of the directive.

As the contents of the menu is loaded from a server file and changes frequently, I don't think I can use a default route, because the route would change name each time the file changed.

events.html

<ul>
   <li ng-repeat="event in events"><a href="#/{{event.title}}">{{event.title}}</a></li>
</ul>

The following list items gets loaded dynamically on page load, but I have shortened it for brevity to load events in-line.

controller.js

 myApp.controller('controller', ['$scope',function($scope){    
   $scope.events = {title: 'event 1', title: 'event 2'};
 }]);

I'd like to have a menu item selected by default when the page loads, such as $scope.events[0].

The Count
  • 43
  • 9
Craig
  • 381
  • 5
  • 22
  • Are you saying you want to load a particular route by default? – azium Jun 11 '15 at 22:30
  • possible duplicate of [Default route for angular ui router](http://stackoverflow.com/questions/22233251/default-route-for-angular-ui-router) – Kirill Slatin Jun 12 '15 at 03:59
  • No, the menu is not for navigating a view, the menu is independent of the view & routing. The menu is used for defining something in a directive, for which I'd like a default menu item selected when the page loads. For example, $scope.events[0]. The events data is pulled from the server and will change regularly. – Craig Jun 14 '15 at 21:47
  • What do you mean by "selected"? The selected `
  • ` has a different class so it looks different, or what? And can the user change the selection? If so, by doing what? I suppose it's not clicking the `` because you say "it passes the title to ... and a view is populated".
  • – ycsun Jun 16 '15 at 08:52
  • I do apologise, my description is wrong. I'm populating the contents of a directive as a result of the menu selection, not a view. – Craig Jun 16 '15 at 10:19
  • The directive that is being populated reads the $routeParams to work out what data to load. I don't think it's very elegant, but what I have currently works except for the default menu selection. – Craig Jun 16 '15 at 10:31