Javascript RequireJS和AngularJS多控制器
我正在使用最新版本的Require和Angular,但我遇到了奇怪的bug。每个视图都有控制器,但显然只对一个视图有效。这是我的示例代码: 定义所有控制器:controllers.jsJavascript RequireJS和AngularJS多控制器,javascript,angularjs,requirejs,angularjs-controller,Javascript,Angularjs,Requirejs,Angularjs Controller,我正在使用最新版本的Require和Angular,但我遇到了奇怪的bug。每个视图都有控制器,但显然只对一个视图有效。这是我的示例代码: 定义所有控制器:controllers.js define([ 'modules/index/controller', 'modules/test/controller' ], function(){}); define(['angular'], function(angular){ 'use strict'; return a
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
这里只适用于一个控制器,如果像这里这样包括2个,我会得到错误:ng:areq
错误的论点
索引:controller.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
测试:controller.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
我错在哪里?问题是您创建了两次
myApp.controllers
模块,其中一个正在覆盖另一个。假设您在加载控制器后手动引导angular,您应该首先创建模块,然后加载该模块以创建控制器:
app.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
索引:controller.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
测试:controller.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
我创建了angularAMD
,以方便您使用可能感兴趣的RequreJS和AngularJS: