我正在使用最新版本的Require和Angular,但是遇到了奇怪的错误。我对每个视图都有控制器,但显然仅对一个视图有效。这是我的示例代码:
定义所有控制器:Controllers.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
这里仅适用于一个控制器,如果我在此处包括2,则会出现错误:ng:areq错误参数
索引:controller.js
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
测试:controller.js
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
我哪里错了?
问题是您要创建该myApp.controllers
模块两次,而一个正在覆盖另一个。假设您在加载控制器后手动启动了angular,那么您应该做的是先创建模块,然后加载该模块以创建控制器:
app.js
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
索引:controller.js
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
测试:controller.js
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
我创建的目的angularAMD
是方便您使用RequreJS和AngularJS:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句