【问题标题】:How to call a method when routing to a template in AngularJS?在AngularJS中路由到模板时如何调用方法?
【发布时间】:2014-04-10 12:10:42
【问题描述】:

在AngularJS中路由到模板时如何从服务中调用方法?

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

这不正确吗? :S

【问题讨论】:

标签: angularjs angularjs-service route-provider


【解决方案1】:

Resolve用于向控制器传递附加参数,例如:

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

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);
});

【讨论】:

  • 酷,我也可以将它发送到服务?还是不行?
  • 您可以像这样在控制器中使用您的服务:wikiApp.controller('myCtrl', function(someParameter, faqService, newsService){...
猜你喜欢
  • 1970-01-01
  • 2016-02-27
  • 2016-09-09
  • 2015-05-12
  • 2013-04-29
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多