Javascript 如何从指令&x27;调用依赖注入服务的嵌套方法;谁的控制器?
我得到的错误是“TypeError:helperService.formatDate不是函数”Javascript 如何从指令&x27;调用依赖注入服务的嵌套方法;谁的控制器?,javascript,angularjs,datetime,angularjs-directive,angularjs-scope,Javascript,Angularjs,Datetime,Angularjs Directive,Angularjs Scope,我得到的错误是“TypeError:helperService.formatDate不是函数” function myController($scope,helperService) { function getFormattedDT() { var localDate = "2016-04-04 12:55:55"; var inputDate = helperService.parsedDate(helperService.formatDate(local
function myController($scope,helperService)
{
function getFormattedDT() {
var localDate = "2016-04-04 12:55:55";
var inputDate = helperService.parsedDate(helperService.formatDate(localDate));
}
getFormattedDT();
}
但是如果我在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 ....
}
}(angular.module('myApp')
angular
服务
注册需要一个返回要使用的对象的方法。您的助手服务
应该返回一个包含方法的对象,如下所示:
(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')
angular
服务
注册需要一个返回要使用的对象的方法。PosthelperService
请输入代码。如果parsedDate
方法可用,我想一定是输入错误。你能用你的全部代码创建一个plnkr吗?@Huguesstevanski我已经用正确的错误消息更新了。拥有helperService
代码是能够进行更深入分析的必要条件。您发布的代码是完全有效的,因此错误必须是there@HuguesStefanski我已经添加了helperService codePosthelperService
代码。如果parsedDate
方法可用,我想一定是输入错误。你能用你的全部代码创建一个plnkr吗?@Huguesstevanski我已经用正确的错误消息更新了。拥有helperService
代码是能够进行更深入分析的必要条件。您发布的代码是完全有效的,因此错误必须是there@HuguesStefanski我已经添加了helperService代码