2013-12-11 89 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没有任何意义。 –