Javascript 参数';fn';不是函数get字符串

Javascript 参数';fn';不是函数get字符串,javascript,html,angularjs,Javascript,Html,Angularjs,我的angular应用程序中有一个部分绑定了一个控制器, 从那时起,我得到了参数'fn'不是一个函数错误,有人能看看我的代码并解释我为什么会出现这个错误吗 我将非常感激:) html标记: {{“消息”|翻译} {{message} 控制器: (函数(){ "严格使用",; var controllers=angular.module('portal.controllers'); controller.controller('MessageController',['$scope','M

我的angular应用程序中有一个部分绑定了一个控制器,
从那时起,我得到了参数'fn'不是一个函数错误,有人能看看我的代码并解释我为什么会出现这个错误吗

我将非常感激:)

html标记:


{{“消息”|翻译}
  • {{message}
控制器:

(函数(){
"严格使用",;
var controllers=angular.module('portal.controllers');
controller.controller('MessageController',['$scope','MessageService','$rootScope',函数MessageController($scope,MessageService,$rootScope){
$rootScope.MSG=MessageService.getMessages();
$rootScope.$watch('MSG',函数(newValue){
$scope.MSG=newValue;
});
}]);
}());
服务:

(函数(){
"严格使用",;
var messageServices=angular.module('portal.services');
工厂('MessageService',['MessageData','localStorageService','UserService',],函数(MessageData,localStorageService,UserService){
返回新的MessageService(MessageData、localStorageService、UserService);
});
功能MessageService(MessageData、localStorageService、UserService){
this.messageData=messageData;
this.localStorageService=localStorageService;
this.userService=userService;
}
MessageService.prototype.getMessages=函数(){
var locale=this.userService.getUserinfoLocale();
var messages=this.localStorageService.get(Constants.key\u messages+locale);
如果(消息!==null&&messages!==未定义){
返回JSON.parse(消息);
}否则{
返回this.messageData.query({
地点:地点
},$.proxy(函数(数据、区域设置){
save(Constants.key_messages+locale,JSON.stringify(data));
}这),;
}
};
MessageService.prototype.save=函数(键,值){
this.localStorageService.add(key,value);
};
}());
数据:

(函数(){
"严格使用",;
var data=angular.module('portal.data');
data.factory('MessageData',函数($resource){
返回$resource(Constants.url_消息,{}{
查询:{
方法:“GET”,
参数:{
区域设置:“区域设置”
},
伊萨雷:是的
}
});
});
}());
html头中js文件的顺序:

<script src="js/lib/jquery-1.10.js"></script>
<script src="js/lib/angular.js"></script>
<script src="js/lib/angular-resource.js"></script>
<script src="js/lib/angular-translate.js"></script>
<script src="js/lib/angular-localstorage.js"></script>
<script src="js/lib/jquery-cookies.js"></script>
<script src="js/lib/bootstrap.js"></script>
<script src="js/portal.js"></script>

问题在于使用“错误”语法创建服务
而不是使用:

messageServices.factory('MessageService', 
    ['MessageData','localStorageService', 'UserService'], 
    function(MessageData, localStorageService, UserService){
        return new MessageService(MessageData, localStorageService, UserService);
    }
);
我必须使用:

messageServices.factory('MessageService', 
    ['MessageData','localStorageService', 'UserService', 
    function(MessageData, localStorageService, UserService){
        return new MessageService(MessageData, localStorageService, UserService);
    }
]);

我很快就用参数关闭了数组,由于我仍在学习,我没有直接看到它,无论如何,我希望我能帮助其他偶然发现它的人。

今天我犯了同样的错误,犯了那个愚蠢的错误:

(function(){

  angular
    .module('mymodule')
    .factory('myFactory', 'myFactory');   // <-- silly mistake 

  myFactory.$inject = ['myDeps'];
  function myFactory(myDeps){
    ...
  }

}());
(函数(){
有棱角的
.module('mymodule')

.factory('myFactory','myFactory');//上述答案在很大程度上帮助我纠正了应用程序中因不同原因产生的相同问题

在构建时,我的客户端应用程序正在连接和缩小,因此我专门编写Angular来避免相关问题

config.$inject = [];
function config() {
    // config stuff
}
(我定义一个函数,
$inject
将其作为一个模块并声明它是什么)

然后我尝试注册
config
,就像我在我的应用程序中注册其他模块一样(控制器、指令等)

这是错误的,并给了我上述错误。所以我改为

angular.module("app").config(config);
它成功了。
我猜angular开发者打算
config
拥有一个单一的实例,因此在注册
config
时angular不接受一个名称。

我也有同样的问题,在我的例子中,问题是angular-cookies.js文件。它与其他angularjs脚本一起放在文件夹中,当我使用gulp缩小我的js文件时发生了一个错误

简单的解决方案是将angular-cookies.js文件放在另一个文件夹中,在所选文件夹之外缩小js文件。

我的案例

  let app: any = angular.module("ngCartosServiceWorker"),
    requires: any[] = [
      "$log",
      "$q",
      "$rootScope",
      "$window",
      "ngCartosServiceWorker.registration",
      PushNotification
    ];
  app.service("ngCartosServiceWorker.PushNotification");
我忘了将requires数组作为参数添加到这样的服务中

app.service("ngCartosServiceWorker.PushNotification", requires);

可能与jQuery有关,比如在使用
$.fn.extend向jQuery添加插件时({…
。也许你还没有包括jQuery libjQuery是第一个添加到我的head标记中的脚本,我将按照js文件的顺序编辑这个问题。这真是太糟糕了。在我的例子中,我错误地将一个服务放在方括号外(因此所有括号和方括号都匹配得很好),我花了很长时间才弄明白,因为我重构了很多代码。所以,同样,当你有类似这样的东西时,你会得到同样的毫无意义的错误消息:
app.factory('x','y',['z',function(y,z){…}]);
(注意y在方括号之外)。最糟糕的是,它不会告诉您哪个文件有此问题。因此,如果您更改了很多文件,请参考figure.or
config('someFactory','otherFactory',superFunction)
而不是
config(['someFactory','otherFactory',superFunction])
在我的情况下,删除方括号中的依赖项名称会有所帮助。就像这样
工厂('historyentries',function($http){}
而不是
.factory('historyentries',['$http']),function($http)
这些行是不需要的,如果您删除了它们,那么它会自动工作
'MessageData','localStorageService','UserService',
对于Run方法也是一样的!非常感谢您分享这一点!
  let app: any = angular.module("ngCartosServiceWorker"),
    requires: any[] = [
      "$log",
      "$q",
      "$rootScope",
      "$window",
      "ngCartosServiceWorker.registration",
      PushNotification
    ];
  app.service("ngCartosServiceWorker.PushNotification");
app.service("ngCartosServiceWorker.PushNotification", requires);