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

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

问题描述

我在我的角度应用程序中绑定了一个控制器,
,因为我得到了参数'fn'不是函数错误,任何人都可以查看我的代码并解释为什么我得到那个错误?



我会很格拉:)

html-markup:

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

控制器:

 (function(){
'use strict';

var controllers = angular.module('portal.controllers');
$ b controllers.controller('MessageController',['$ scope','MessageService','$ rootScope',函数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' );
$ b messageServices.factory('MessageService',['MessageData','localStorageService','UserService'],函数(MessageData,localStorageService,UserService){
return new MessageService(MessageData ,localStorageService,UserService);
});

函数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&&& amp;& message !==未定义){
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');
$ b data.factory('MessageData',function($ resource){
return $ resource(Constants.url_messages,{},{
query:{
method:' GET',
params:{
locale:'locale'
},
isArray:true
}
});
}) ;
}());

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'],
函数(MessageData,localStorageService,UserService){
返回新的MessageService(MessageData,localStorageService,UserService);
}
);

我不得不使用:

<$ p $消息服务',
['MessageData','localStorageService','UserService',
函数(MessageData,localStorageService,UserService){
返回新的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天全站免登陆