参数 'fn' 不是一个函数得到的字符串 [英] Argument 'fn' is not a function got string

查看:27
本文介绍了参数 'fn' 不是一个函数得到的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 angular 应用程序中绑定了一个控制器,
从那以后我得到了参数fn"不是函数错误,谁能看看我的代码并解释我为什么会得到这个错误?

我将不胜感激:)

html 标记:

<字段集><图例>{{ 'MESSAGES' |翻译}}</legend></fieldset><div class="margin-left-15"><ul class="list-style-button"><li data-ng-repeat="message in MSG">{{ message }}</li>

</节>

控制器:

(function() {'使用严格';var 控制器 = angular.module('portal.controllers');controller.controller('MessageController', ['$scope', 'MessageService', '$rootScope', function MessageController($scope, MessageService, $rootScope) {$rootScope.MSG = MessageService.getMessages();$rootScope.$watch('MSG', function(newValue) {$scope.MSG = newValue;});}]);}());

服务:

(function() {'使用严格';var messageServices = angular.module('portal.services');messageServices.factory('MessageService', ['MessageData', 'localStorageService', 'UserService'], function(MessageData, localStorageService, UserService) {返回新消息服务(消息数据,本地存储服务,用户服务);});功能消息服务(消息数据,本地存储服务,用户服务){this.messageData = MessageData;this.localStorageService = localStorageService;this.userService = 用户服务;}MessageService.prototype.getMessages = function() {var locale = this.userService.getUserinfoLocale();var messages = this.localStorageService.get(Constants.key_messages + locale);if (messages !== null && messages !== undefined) {返回 JSON.parse(messages);} 别的 {返回 this.messageData.query({语言环境:语言环境}, $.proxy(function(data, locale) {this.save(Constants.key_messages + locale, JSON.stringify(data));}, 这个));}};MessageService.prototype.save = function(key, value) {this.localStorageService.add(key, value);};}());

数据:

(function() {'使用严格';var data = angular.module('portal.data');data.factory('MessageData', function($resource) {返回 $resource(Constants.url_messages, {}, {询问: {方法:'获取',参数:{语言环境:'语言环境'},isArray: 真}});});}());

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'],功能(消息数据,本地存储服务,用户服务){返回新消息服务(消息数据,本地存储服务,用户服务);});

我不得不使用:

messageServices.factory('MessageService',['MessageData','localStorageService','UserService',功能(消息数据,本地存储服务,用户服务){返回新消息服务(消息数据,本地存储服务,用户服务);}]);

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

I have a part in my angular application on which I've binded a controller,
since then I got the Argument 'fn' is not a function Error, can anyone look at my code and explain why I got that Error?

I would be very gratefull :)

html-markup:

<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;
    });
  }]);
}());

Service:

(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);
  };

}());

data:

(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
      }
    });
  });
}());

order of js files in html head:

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

解决方案

The problem was in using the 'wrong' syntax to create the service
instead of using:

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

I had to use:

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

I closed the array with parameters to soon, and since I'm still learning I didn't see it directly, anyhow I hope I can help others who stumble upon this.

这篇关于参数 'fn' 不是一个函数得到的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆