angular js error:参数'questionsCtrl'不是函数

时间:2016-06-28 08:45:38

标签: javascript jquery angularjs

我试图在ui加载时调用控制器,但是低于错误。

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

我的代码是:

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');
    }

}());

1 个答案:

答案 0 :(得分:3)

定义控制器时需要参考该功能。

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

controller()的第一个参数是控制器的名称,第二个是映射。

我建议使用以下最小安全语法。

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

}]);

参考控制器功能

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

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