[英]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" . 我收到“ 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')
But if i keep the function in the myController, then it works well. 但是,如果我将函数保留在myController中,那么它将很好地工作。 How do i call nested methods in injected dependencies of angular. 我如何在angular的注入依赖项中调用嵌套方法。
Your helperService
should return an object with the methods on it, something like this: 您的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')
The angular
service
registration expects a method that returns an object to be used. angular
service
注册需要一种返回要使用的对象的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.