gpt4 book ai didi

javascript - 参数 'fn' 不是函数得到的字符串

转载 作者:IT王子 更新时间:2023-10-29 02:52:21 26 4
gpt4 key购买 nike

我在我的 Angular 应用程序中有一部分绑定(bind)了 Controller ,
从那时起我得到了 Argument 'fn' is not a function 错误,任何人都可以看看我的代码并解释为什么我得到那个错误吗?

我将不胜感激:)

html 标记:

<section class="col-lg-12" data-ng-controller="MessageController">
<fieldset>
<legend>{{ 'MESSAGES' | translate }}</legend>
</fieldset>
<div class="margin-left-15">
<ul class="list-style-button">
<li data-ng-repeat="message in MSG">{{ message }}</li>
</ul>
</div>
</section>

Controller :

(function() {
'use strict';

var controllers = angular.module('portal.controllers');

controllers.controller('MessageController', ['$scope', 'MessageService', '$rootScope', function MessageController($scope, MessageService, $rootScope) {
$rootScope.MSG = MessageService.getMessages();

$rootScope.$watch('MSG', function(newValue) {
$scope.MSG = newValue;
});
}]);
}());

服务:

(function() {

'use strict';

var messageServices = angular.module('portal.services');

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

function MessageService(MessageData, localStorageService, UserService) {
this.messageData = MessageData;
this.localStorageService = localStorageService;
this.userService = UserService;
}

MessageService.prototype.getMessages = function() {
var locale = this.userService.getUserinfoLocale();
var messages = this.localStorageService.get(Constants.key_messages + locale);
if (messages !== null && messages !== undefined) {
return JSON.parse(messages);
} else {
return this.messageData.query({
locale: locale
}, $.proxy(function(data, locale) {
this.save(Constants.key_messages + locale, JSON.stringify(data));
}, this));
}
};

MessageService.prototype.save = function(key, value) {
this.localStorageService.add(key, value);
};

}());

数据:

(function() {
'use strict';

var data = angular.module('portal.data');

data.factory('MessageData', function($resource) {
return $resource(Constants.url_messages, {}, {
query: {
method: 'GET',
params: {
locale: 'locale'
},
isArray: true
}
});
});
}());

html head中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);
}
]);

我很快就关闭了带参数的数组,由于我还在学习我没有直接看到它,无论如何我希望我能帮助那些偶然发现这个的人。

关于javascript - 参数 'fn' 不是函数得到的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095129/

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