22

I have the following code;

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', ...);

I get the following error

Uncaught ReferenceError: SomeControl is not defined from myApp

Is the problem just that I can not use app.controller('SomeControl', ...) syntax? when using $routeProvider? is the only working syntax:

function SomeControl(...)
2

3 Answers 3

44

Use quotes:

            controller: 'SomeControl'
Sign up to request clarification or add additional context in comments.

1 Comment

DUH, Thanks for finding my small mistake ;)
8

Like Foo L said, you need to put quotes around SomeControl. If you don't use quotes, you are referring to the variable SomeControl, which is undefined because you did not use a named function to represent the controller.

When you use the alternative that you mentioned, function SomeControl(...), you define that named function. Otherwise, Angular needs to know that it needs to look up the controller in the myApp module.

Using the app.controller('SomeControl', ...) syntax is better because it does not pollute the global namespace.

Comments

1

The above answers are correct however, this error can also happen :

  1. If the name of the controller in you html or jsp etc page is not matching the actual cotnroller

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

  1. Also if the name of the function controller does not match the controller definition then this error can happen too.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.