gpt4 book ai didi

javascript - 如何从指令的 Controller 调用依赖注入(inject)服务的嵌套方法?

转载 作者:行者123 更新时间:2023-11-28 06:10:49 24 4
gpt4 key购买 nike

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

But if i keep the function in the myController, then it works well. How do i call nested methods in injected dependencies of 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 注册需要一个返回要使用的对象的方法。

关于javascript - 如何从指令的 Controller 调用依赖注入(inject)服务的嵌套方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com