codememo

인수 'fn'이 함수 get 문자열이 아닙니다.

tipmemo 2023. 3. 9. 22:06
반응형

인수 'fn'이 함수 get 문자열이 아닙니다.

콘트롤러를 묶은 부분이 있어요
인수 'fn'은 함수가 아닙니다.누구라도 내 코드를 보고 왜 오류가 발생했는지 설명해 줄 수 있나요?

정말 감사합니다. :)

html-module:

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

컨트롤러:

(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 헤드의 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>

'wrong' 구문을 사용하여 서비스를 생성하는 데 문제가 발생했습니다.
다음을 사용하는 대신:

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){
    ...
  }

}());

그 대신:

(function(){

  angular
    .module('mymodule')
    .factory('myFactory', myFactory);   // <-- right way to write it    

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

}());

실제로 문자열이 아닌 함수를 기다리는 인젝터에 문자열 "myFactory"가 들어왔습니다.이것으로 [ng:areq]오류가 설명되었습니다.

위의 답변은 다른 원인에 의해 발생한 어플리케이션과 동일한 문제를 수정하는 데 큰 도움이 되었습니다.

빌드 시 클라이언트 앱이 연결 및 최소화되고 있기 때문에 관련 문제를 피하기 위해 특별히 Angular를 쓰고 있습니다.내가 정의한다config다음과 같이

config.$inject = [];
function config() {
    // config stuff
}

(나는 함수를 정의한다.$inject모듈로 간주하여 선언합니다.

그리고 나서 나는 등록하려고 했다config내 앱에 다른 모듈(컨트롤러, 디렉티브 등)을 등록한 것처럼.

angular.module("app").config('config', config); // this is bad!

// for example, this is right
angular.module("app").factory('mainService', mainService);

이것은 잘못된 것이며, 앞서 말한 오류를 내게 주었다.그래서 나는 로 바꿨어요.

angular.module("app").config(config);

그리고 그것은 성공하였다.각진 데브들이 의도한 건config예를 들어 Angular가 이름을 받아들이지 않도록 한다.config등록되었습니다.

저도 같은 문제가 있었습니다만, 제 경우는 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");

이와 같은 서비스를 제공하기 위해 필요한 어레이를 파라미터로 추가하는 것을 잊었습니다.

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

언급URL : https://stackoverflow.com/questions/19095129/argument-fn-is-not-a-function-got-string

반응형