An easy way to do this is by using ng-init in the appropriate template .
angular.module('demo', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/linkOne', { controller: 'LinkController', template: '<h1 ng-init="functionOne()">Hello world</h1>' }) .when('/linkTwo', { controller: 'LinkController', template: '<h1 ng-init="functionTwo()">Hello Stackoverflow</h1>' }); }]) .controller('LinkController', ['$scope', function($scope) { $scope.functionOne = function(){ console.log("Hello"); } $scope.functionTwo = function(){ console.log("Hello world"); } }]);
HTML code:
<!DOCTYPE html> <html ng-app="demo"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script> <script src="https://code.angularjs.org/1.4.6/angular-route.js"></script> </head> <body> <a href="#linkOne">Link One</a> <a href="#linkTwo">Link Two</a> <div ng-view></div> </body> </html>
source share