我在我的js文件中定义了一个Controller,如下所示。这一切都很完美,直到我在我的javascript文件中更改“Test2Controller”控制器的位置。当我将Test2Controller放在SampleController函数/函数声明下面时,它给出了以下错误:
Chrome开发工具中的错误:
Uncaught TypeError:对象#的属性'controller'不是a function mycontroller1.js:26错误:参数'Test2Controller'不是 一个函数,未定义 在assertArg(文件:/// D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1039:11) 在assertArgFn(file:/// D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1049:3) 在file:/// D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4802:9 在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) 在compositeLinkFn(file:/// D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4015:15) 在compositeLinkFn(file:/// D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4018:13) 在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 :(得分:1)
controller
是myModule
的属性或类型函数。这就是你可以使用
myModule.controller(...);
但是在您的代码中,您将此属性替换为string:
类型的值myModule.controller = 'SampleController' ;
很明显,之后你不能调用函数myModule.controller()
,因为myModule.controller
不再是函数了。
删除行myModule.controller = 'SampleController';
:它没有任何意义。