aliaz
aliaz

Reputation: 787

AngularJS- Route to a page with an item selected

I have a page as shown in this plnkr example.

http://plnkr.co/edit/NKVVn4ga6lYOBWEblt0o?p=preview

When I click on a hyperlink(added on bottom of page) with href="#/test", I want it to open this page but with the one of the items in the list selected.

(The reason for trying to acheive this is I will have a url with this route in a different app and when clicking on the link I want this page open with the item selected.)

This is what my routeProvider code looks like

myItemsApp.config(['$routeProvider', function($routeProvider){
  $routeProvider.
    when('/test',{
      templateUrl:'index.html',
    }).
    otherwise({
      redirectTo:'/',
    })
}]); 

Can I do anything in here to set the third item (for example) in the list selected?

What I have done so far is I have tried doing a resolve in the routeProvider and some logic in the controller to set the third item in the list to be selected and has failed miserably.

Any help would be greatly appreciated.

Upvotes: 0

Views: 447

Answers (1)

Dayan Moreno Leon
Dayan Moreno Leon

Reputation: 5435

this is actually very simple, if you already have your route just use $routeParams to get the handle on the selected item id and then use that id to traverse your list and set the right element as active, as for your route, i don't recall ngRoute cause i haven't use it in a while, but you can just create other route, same controller, same template just make the url look like /test/:item_id

Upvotes: 0

Related Questions