pandu das pandu das - 5 months ago 23
jQuery Question

angular js error : Argument 'questionsCtrl' is not a function

I am trying to call a controller when ui loads, but getting below error.

angular.js:9778 Error: [ng:areq] Argument 'questionsCtrl' is not a function, got string
http://errors.angularjs.org/1.2.16/ng/areq?p0=questionsCtrl&p1=not%20aNaNunction%2C%20got%string
at http://127.0.0.1:60668/js/angular.js:78:12
at assertArg (http://127.0.0.1:60668/js/angular.js:1443:11)
at assertArgFn (http://127.0.0.1:60668/js/angular.js:1453:3)
at http://127.0.0.1:60668/js/angular.js:7131:9
at http://127.0.0.1:60668/js/angular-ui-router.js:2797:28
at nodeLinkFn (http://127.0.0.1:60668/js/angular.js:6579:13)
at compositeLinkFn (http://127.0.0.1:60668/js/angular.js:5986:15)
at publicLinkFn (http://127.0.0.1:60668/js/angular.js:5891:30)
at updateView (http://127.0.0.1:60668/js/angular-ui-router.js:2733:23)
at http://127.0.0.1:60668/js/angular-ui-router.js:2697:11


my code is:

app.js

(function () {
"use strict";
var app = angular.module("autoQuote",["ui.router"]);

app.config(["$stateProvider","$urlRouterProvider", function($stateProvider,$urlRouterProvider){
$urlRouterProvider.otherwise("/");

$stateProvider
.state("step1", {
url : "/",
templateUrl : "step1.html",
controller: "questionsCtrl"
})
.state("step2", {
url : "/step2",
templateUrl : "step2.html",
controller: "questionsCtrl"
})
}]
);
}());


questionsCtrl.js

(function () {
"use strict";

angular
.module("autoQuote")
.controller("questionsCtrl",["questionsCtrl"]);

function questionsCtrl() {
console.log('here in questionsCtrl');
}

}());

Answer

You need to refer the function when defining the controller.

.controller("questionsCtrl", questionsCtrl);
                             ^^^^^^^^^^^^^     : Function reference, not a string

The first parameter to the controller() is the name of the controller and second is the mappings.


I'll suggest to use following min-safe syntax.

.controller('questionsCtrl', ['$scope', function ($scope) {

}]);

To refer to controller function

.controller('questionsCtrl', ['$scope', questionsCtrl]);

function questionsCtrl($scope) {
    console.log('In questionsCtrl');
}
Comments