我在我的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和有點在這裏。這裏可能是什麼問題?
爲什麼不使用相同的控制器語法的SampleController –