itsource

angularjs [ng:areq] 인수 'fn'이 함수가 아닙니다. 문자열이 있습니다.

mycopycode 2023. 10. 24. 21:19
반응형

angularjs [ng:areq] 인수 'fn'이 함수가 아닙니다. 문자열이 있습니다.

저는 angular js가 처음입니다.오류가 발생합니다. 도와주세요.

[ng:areq] 'fn' 인수가 함수가 아닙니다. 문자열이 있습니다.

var app = angular.module('demo',[]);


app.config('$routeProvider',function($routeProvider){

    $routeProvider.when('/add',{
        templateUrl:'demo/add.html',
        controller:'addcontroller'
    }).
    when('/order',{
        templateUrl:'demo/order.html',
        controller:'ordercontroller'
    });

});

app.controller('addcontroller',function($scope){
    $scope.message="order";
});
app.controller('ordercontroller',function($scope){
    $scope.message="order";
});

구성 블록에 오류가 있는 것 같습니다. 다음 중 하나여야 합니다.

app.config(function($routeProvider){
  // routeProvider config
});

더 나은 경우:

app.config(['$routeProvider', function($routeProvider){
  // routeProvider config, allows minification
}]);

주석은 미니메이션이 올바르게 작동할 수 있도록 하기 위한 것입니다.Angular에서 자세히 보실 수 있습니다.JS docs https://docs.angularjs.org/tutorial/step_05 이 연습은 앱 전체에서 제대로 작동하기 위해 수행되어야 합니다.

이 질문의 맥락과 직접적으로 관련되지는 않지만, 이 오류 메시지는 다음과 같이 함수가 아닌 다른 것을 반환하는 확인 블록에 의해 발생할 수도 있습니다.

$stateProvider.state('mystate', {
    url: "/myurl",
    templateUrl: "mytemplate.html",
    controller: "MyController",
    resolve: {
        authenticatedUser: securityAuthorizationProvider.requireAuthenticatedUser
    }
});

한다면securityAuthorizationProvider.requireAuthenticatedUser정의되지 않은 경우 이 오류가 발생할 수 있습니다.

이것이 오래된 게시물이라는 것을 알고 있지만, 이 정확한 오류로 내 코드에 문제가 있는 것에 기여할 것이라고 생각했습니다.이런 식으로 컨트롤러에 서비스를 주입했습니다.

theApp.directive('theDirective', 'myService', ['$http', function ($http, myService) {}]);

이 대신:

theApp.directive('theDirective', ['$http', 'myService', function ($http, myService) {}]);

인라인 배열 주석 외부에 내 서비스가 포함되어 있음을 주목하십시오!그것은 제 입장에서 너무 시간을 많이 들인 뼈있는 행동이었습니다.

문제는 어레이 외부에서 공장 의존성을 정의하는 것이었습니다.아마도 미니어처 또한 문제였을 것입니다.

//Error in this
  angular
    .module('mymodule').factory('myFactory', 'thisconstant', function (thisconstant) {

});

이는 다음과 같습니다.

//correct code
  angular
    .module('mymodule').factory('myFactory', ['thisconstant', function (thisconstant) {

}]);

여기에 다양한 활용 사례가 추가됐기 때문에 저도 추가해야겠다고 생각했습니다.기능 서비스를 재구성할 때 이 오류가 발생했습니다.

angular
.app('myApp')
.service('MyService', function (){
  // do something
return MyService;
})

클래스 개체로:

export default class MyService { ... }

angular
    .app('myApp')
    .service('MyService', MyService);

문제는 기능이 종료될 때 서비스가 자동으로 반환된다는 것과 클래스 기능을 추가해야 한다는 것이었습니다. 이 기능은 다음과 같습니다.

export default class MyService {
  constructor(){
  // doing something
  }

  get() {
  return MyService;
  }
}

문제를 해결해 줬습니다:)

저는 자바스크립트에서 익명 함수와 명명 함수를 취급하는 방식을 오해하여 이 오류를 얻었습니다.

이로 인해 다음과 같은 오류가 발생합니다.

angular.module("app").factory("myDataService", ['$resource', myDataService]);
var myDataService = function($resource) {
    return $resource('/url');
}

이렇게 할 걸 그랬어요

var myDataService = function($resource) {
    return $resource('/url');
}
angular.module("app").factory("myDataService", ['$resource', myDataService]);

또는 이것:

angular.module("app").factory("myDataService", ['$resource', myDataService]);
function myDataService($resource) {
    return $resource('/url');
}

익명 함수와 명명 함수의 차이에 대한 자세한 내용은 여기에 나와 있습니다.

첫 번째 줄에서는 이렇게 써야 합니다.

var app = angular.module('demo', ['ngRoute']);

그리고 이런 라우팅 방법을 사용하면,

$routeProvider.when('/add').then({
        templateUrl:'demo/add.html',
        controller:'addcontroller'
    });

이 단계를 한 번만 시도해보세요.

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

반응형