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.

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, this works perfectly. I didn't know about the resolve property as I'm relatively new. Thanks!
No problem, glad I could help!
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.