2016-06-28 47 views
0

我想調用一個控制器,當用戶界面加載,但得到低於錯誤。角js錯誤:參數'questionsCtrl'不是函數

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

}()); 
+0

你正確導入'questionsCtrl.js'在HTML頁面中? – Mistalis

+0

是的,js文件包含正確。 –

回答

3

定義控制器時,您需要參考的功能。

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

controller()第一個參數是所述控制器的名稱和第二個是映射。


我會建議使用以下min-safe語法。

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

}]); 

要引用控制器功能

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

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