How to call a function in AngularJs when the route matches?

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

displays a simple list in which each entry can be selected. However, when you select additional viewing, it does not load. Everything in the ticket_lost template . The template has several hidden fields that appear when the button is clicked.

I can determine which record 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 this possible? How to do it? What do I need to change in routing?

+7
source share
1 answer

See the $ routeParams service. It allows you to configure a route with parameters that will be processed by the 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(); }); 
+20
source

All Articles