简体   繁体   中英

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

I am getting the error as "TypeError : helperService.formatDate is not a function" .

(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')

But if i keep the function in the myController, then it works well. How do i call nested methods in injected dependencies of angular.

Your helperService should return an object with the methods on it, something like this:

(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')

The angular service registration expects a method that returns an object to be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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