6

I am using $routeProvider to route all of my apps pages to the correlating controllers, but when two routes use the same controllers (i.e. /blog & /blog:id) how can I initialize a separate function depending on the current route.

So if the route is /blog I want to initialize $scope.loadPosts() on route load. If the route is /blog:id I want to initialize $scope.loadPost($id) on route load.

Polarize
  • 1,005
  • 2
  • 10
  • 27
  • did you check this one http://stackoverflow.com/questions/16062434/angular-different-route-same-template-controller-different-loading-method or is it different from what you want? – Sreekumar SH Oct 29 '15 at 02:25

2 Answers2

4

You could do a few things here, but my suggestion would be to pass the initialization function through the resolve of the route. You could do something like this:

angular.module('test', ['ngRoute'])
  .config(['$routeProvider',
    function($routeProvider) {
      $routeProvider
        .when('/blog', {
          controller: 'BlogController',
          template: '<h1>Blog</h1>',
          resolve: {
            init: function() {
              return function() {
                console.log('Loading Blog');
              }
            }
          }
        })
        .when('/blog/:id', {
          controller: 'BlogController',
          template: '<h1>Blog ID</h1>',
          resolve: {
            init: function() {
              return function($route) {
                console.log('Loading Blog Article ' + $route.current.params.id);
              }
            }
          }
        });
    }
  ])
  .controller('BlogController', ['$scope', '$route', 'init',
    function($scope, $route, init) {
      init($route);
    }
  ]);
<!DOCTYPE html>
<html ng-app="test">

<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="#/blog">Go to Blog</a>
  <a href="#/blog/2">Go to Article 2</a>

  <div ng-view></div>
</body>

</html>

Some other options would be:

  • Resolve some sort of indicator that you could do a switch on in an initialization function inside the controller
  • If you're loading the blog route, resolve an undefined object, if you're loading the blog:id route, resolve the actual item you would load for that route

Hopefully this code gets you started on the right track with whatever you decide.

edit Here's a link to a plunker, the code snippet appears to be acting oddly intermittently

Ben Black
  • 3,751
  • 2
  • 25
  • 43
3

The Simple way to do this is, using ng-init in corresponding 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>
Amaresh C.
  • 579
  • 5
  • 17