7
$routeProvider.when('/ticket', {
    controller: TicketController, 
    templateUrl: Routing.generate('ticket_list')
});

displays a simple list where each entry is selectable. However on select no extra view is loaded. Every thing is in ticket_lost template. The template has some hidden fields that are revealed when entry is clicked.

I can define which entry is selected internally by setting

selectedTicket = 1;

So when there is a route like

/ticket/1

I want to call a function that sets selectedTicket to 1. Is that possible? How to do that? What do I have to change in routing?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • Could you give some more information on your application - you are referring elements that have not be explained in your question - perhaps a plunkr or fiddle too could be provided? – callmekatootie May 05 '13 at 11:44

1 Answers1

20

Take a look at $routeParams service. It allows to set up route with parameters which will be parsed by service:

// Given:
// URL: http://server.com/index.html#/ticket/1
// Route: /ticket/:ticketId
//
// Then
$routeParams ==> {ticketId:1}

In your controller:

angular.module('myApp')
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/ticket', {controller: 'TicketController'});
        $routeProvider.when('/ticket/:ticketId', {controller: 'TicketController'});
        $routeProvider.otherwise({redirectTo: '/ticket'});
    }])
    .controller('TicketController', function ($scope, $routeParams) {
        var init = function () {
            if ($routeParams.ticketId) {
                $scope.ticketSelected($routeParams.ticketId);
            }
        };

        // fire on controller loaded
        init();
    });
Dmitry Evseev
  • 11,533
  • 3
  • 34
  • 48
  • And how do I make my route /ticket work with and without ticketId paramter? – DarkLeafyGreen May 05 '13 at 12:46
  • It should work with 2 routes that use the same Controller, one with :ticketId param, one without – Narretz May 05 '13 at 17:07
  • Yeah, listen to @Narretz : you'll need 2 routes. Updated the answer – Dmitry Evseev May 05 '13 at 17:40
  • @Dmitry Evseev is this the best way to identify the right method to call when the URL changes, is there no better way? You are just checking if ticketId exists in the $routeParams, what if I have lots of URL with `ticketId`s ? – Emeka Mbah Aug 17 '15 at 09:12
  • 1
    @Digitlimit if you have lots of urls having same parameter name `tickerId` but doing different logic, you'd better have a separate controllers for them. One controller with too much logic will be harder to maintain, scale and test. – Dmitry Evseev Aug 17 '15 at 09:51
  • @Dmitry Evseev Yeah you are right. I think with this https://github.com/angular-ui/ui-router, it will be more easier – Emeka Mbah Aug 17 '15 at 10:18