programing

AngularJS UncaughtReferenceError: 컨트롤러가 모듈에서 정의되지 않았습니다.

telecom 2023. 10. 12. 21:47
반응형

AngularJS UncaughtReferenceError: 컨트롤러가 모듈에서 정의되지 않았습니다.

저는 다음 코드를 가지고 있습니다.

var app =
    angular.
        module("myApp",[]).
        config(function($routeProvider, $locationProvider) {
            $routeProvider.when('/someplace', {
                templateUrl: 'sometemplate.html',
                controller: SomeControl
             });
             // configure html5 to get links working on jsfiddle
             $locationProvider.html5Mode(true);
        });

app.controller('SomeControl', ...);

다음 오류가 발생합니다.

Uncaught ReferenceError: SomeControl is not defined from myApp

$routeProvider를 사용할 때 app.controller('SomeControl', ...) 구문을 사용할 수 없다는 것이 문제입니까?는 유일하게 작동하는 구문입니다.

function SomeControl(...)

따옴표 사용:

            controller: 'SomeControl'

Foo L의 말대로 따옴표를 붙여야 합니다.SomeControl. 따옴표를 사용하지 않으면 변수를 참조하는 것입니다.SomeControl, 컨트롤러를 나타내는 데 명명된 함수를 사용하지 않았기 때문에 정의되지 않았습니다.

당신이 말한 대안을 사용할 때,function SomeControl(...), 당신은 그 명명된 함수를 정의합니다.그렇지 않으면 Angular는 컨트롤러를 검색해야 한다는 것을 알아야 합니다.myApp모듈.

사용.app.controller('SomeControl', ...)구문은 전역 네임스페이스를 오염시키지 않기 때문에 더 좋습니다.

위의 답변이 맞지만, 이 오류가 발생할 수도 있습니다.

  1. html 또는 jspetc 페이지의 컨트롤러 이름이 실제 cotcontroller와 일치하지 않는 경우

<div ng-controller="yourControllerName as vm">

  1. 또한 기능 컨트롤러의 이름이 컨트롤러 정의와 일치하지 않을 경우에도 이 오류가 발생할 수 있습니다.

angular.module('smart.admin.vip') .controller('yourController', yourController); function yourController($scope, gridSelections, gridCreationService, adminVipService) { var vm = this; activate();

언급URL : https://stackoverflow.com/questions/17454782/angularjs-uncaught-referenceerror-controller-is-not-defined-from-module

반응형