2013-12-11 63 views
0

我在我的js文件中定義了一個Controller,如下所示。這一切都工作完美,直到我改變了我的JavaScript文件中的「Test2Controller」控制器的位置。當我放置SampleController功能/函數聲明如下Test2Controller它給了我下面的錯誤:控制器功能沒有在js文件中被識別

錯誤在Chrome開發工具:

Uncaught TypeError: Property 'controller' of object # is not a function mycontroller1.js:26 Error: Argument 'Test2Controller' is not a function, got undefined at assertArg (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1039:11) at assertArgFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1049:3) at file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4802:9 at file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4384:17 at forEach (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:137:20) at nodeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4369:11) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4015:15) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4018:13) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4018:13) at publicLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:3920:30) ...

工作::

var myModule = angular.module ('mymodule',[]) ; 

myModule.controller('TestController', function($scope) { 
    $scope.message = 'Message from TestController'; 
}); 

myModule.controller('Test2Controller',function ($scope){ 
    $scope.message = "Message from Test2Controller" ; 
}) ; 

myModule.controller = 'SampleController' ; 

function SampleController($scope) { 
    $scope.customers = [ 
         {name:'AAA',city:'Plano'}, 
         {name:'BBB',city:'Plano'}, 
         {name:'CCC',city:'Bangalore'}, 
         {name:'DDD',city:'SanJose'}, 
         {name:'EEE',city:'SanFO'}, 
         {name:'FFF',city:'SanJose'} 
         ] ; 

    $scope.addCustomer = addCust ; 

    function addCust() { 
     $scope.customers.push ({name:$scope.customerName , city:$scope.customerCity}) ; 
    } ; 
} 

不工作::

var myModule = angular.module ('mymodule',[]) ; 

myModule.controller('TestController', function($scope) { 
    $scope.message = 'Message from TestController'; 
}); 

myModule.controller = 'SampleController' ; 

myModule.controller('Test2Controller',function ($scope){ 
    $scope.message = "Message from Test2Controller" ; 
}) ; 

function SampleController($scope) { 
    $scope.customers = [ 
         {name:'AAA',city:'Plano'}, 
         {name:'BBB',city:'Plano'}, 
         {name:'CCC',city:'Bangalore'}, 
         {name:'DDD',city:'SanJose'}, 
         {name:'EEE',city:'SanFO'}, 
         {name:'FFF',city:'SanJose'} 
         ] ; 

    $scope.addCustomer = addCust ; 

    function addCust() { 
     $scope.customers.push ({name:$scope.customerName , city:$scope.customerCity}) ; 
    } ; 
} 

我目前正在學習角js和有點在這裏。這裏可能是什麼問題?

+0

爲什麼不使用相同的控制器語法的SampleController –

回答

1

controllermyModule的屬性或類型函數。這就是爲什麼你可以使用

myModule.controller(...); 

但在你的代碼,您可以通過字符串類型的值替換此屬性:

myModule.controller = 'SampleController' ; 

所以,很顯然,你不能調用函數myModule.controller()後,因爲myModule.controller不再是一個功能。

刪除行myModule.controller = 'SampleController';:它沒有任何意義。

+0

您好JB Nizet,正如問題中提到的「SampleController」沒有任何問題。這個問題只與「Test2Controller」有關。 「Test2Controller」位於「myModule.controller ='SampleController'之前的第一個代碼片段;」「完美無缺地工作。當它移動到「myModule.controller ='SampleController';」 ,這段代碼會中斷。有什麼建議麼 ??? – J2G

+0

建議:重新閱讀我的答案。你用一個String替換myModule中的函數控制器()。顯然,在替換之後,你不能再調用myModule.controller(),因爲調用一個String沒有任何意義。 –