繁体   English   中英

如何从指令的控制器调用依赖项注入服务的嵌套方法?

[英]How do i call nested methods of dependency injected service from a directive's controller?

function myController($scope,helperService)
  {

   function getFormattedDT() {
      var localDate = "2016-04-04 12:55:55";
      var inputDate = helperService.parsedDate(helperService.formatDate(localDate));
    } 
     getFormattedDT();
   }

我收到“ TypeError:helperService.formatDate不是函数”的错误

(function (myApp)
 {
    myApp.service('helperService',['$http','$q','$sce','miscService', function($http,$q,$sce,'miscService') {

    function formatDate(dateTime) {
        return ....
    }

    function parsedDate(date) {
       return ....
     }

}(angular.module('myApp')

但是,如果我将函数保留在myController中,那么它将很好地工作。 我如何在angular的注入依赖项中调用嵌套方法。

您的helperService应该返回带有其方法的对象,如下所示:

(function (myApp)
  {
    myApp.service('helperService'
     ['$http','$q','$sce','miscService',
      function($http,$q,$sce,'miscService') {

        function formatDate(dateTime) {
          return ....
        }

        function parsedDate(date) {
          return ....
        }

        return{
          parsedDate: parsedDate,
          formatDate: formatDate
        }

  }(angular.module('myApp')

angular service注册需要一种返回要使用的对象的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM