Question

How to call a method from a service when routing to a template in AngularJS?

wikiApp.config(['$routeProvider', 'faqService', 'newsService', 
  function($routeProvider, faqService, newsService) {
  $routeProvider
    .when('/hjem',{
        templateUrl: 'partials/homeContent.html',
    resolve: {
      newsService.setChosenStory("");
      faqService.setChosenFAQ("");
    }
 })

this is not correct? :S

Was it helpful?

Solution

Resolve is used to pass additional parameters to controller, for example:

$routeProvider
.when('/a', {
    templateUrl: '/tmpl',
    controller: 'myCtrl',
    resolve: {
        someParameter: function(){
            return 'konichiva';
        }
    }
})
.when('/b', {
    templateUrl: '/tmpl',
    controller: 'myCtrl',
    resolve: {
        someParameter: function(){
            return 'vatashiva';
        }
    }
});

in myCtrl:

wikiApp.controller('myCtrl', function(someParameter){
    // if path is "/a" then "konichiva" will be logged
    // else if path is "/b" then "vatashiva" will be logged
    console.log(someParameter);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top