如何分离在angularjs Controller文件 [英] How to separate Controller file in angularjs
问题描述
我已经看过分离文件的各种例子。我能理解他们,但问题来了我的code是这样的。我想分开在不同的文件中这些控制器。
使用严格的;
angular.module(MyModule的',['ui.bootstrap']);
VAR对myApp = angular.module('对myApp',[]);myApp.config(['$ httpProvider',函数($ httpProvider){
$ httpProvider.defaults.useXDomain = TRUE;
删除$ httpProvider.defaults.headers.common ['X-要求-随着'];
}]);myApp.config(['$ routeProvider',功能($ routeProvider){
$ routeProvider.when('/ getplaces',{
templateUrl:'模板/ getplaces.html',
控制器:'的ListCtrl
})
的下面控制器需要在不同的文件中。
块引用>/////////////月度数据/////////////////////// //////////////myApp.config(['$ routeProvider',函数($ routeProvider){
$ routeProvider.when('/得到月',{
templateUrl:'模板/ getmacs.html',
控制器:'MonthlyCtrl
})
}])
.controller('MonthlyCtrl',函数($范围,$ HTTP){$ scope.visible = TRUE;
})我有超过20个控制器像上面。但我怎么把它们分开。
解决方案这应该很容易做的,我会安排我的应用程序路由配置到主app.js文件。
myApp.config(['$ routeProvider', 功能($ routeProvider){
$ routeProvider.when('/ getplaces',{
templateUrl:'模板/ getplaces.html',
控制器:'的ListCtrl
})。当('/的getMonth',{
templateUrl:'模板/ getmacs.html',
控制器:'MonthlyCtrl
})
}])然后,当你在每个文件创建一个单独的控制器只是参考应用程序名称这样:
myApp.controller('MonthlyCtrl',['$范围,$ HTTP',函数($范围,$ HTTP){
$ scope.visible = TRUE;
}])您还会注意到我使用数组初始化方式,当你正在做微小这会节省你一些hastles。
I have looked at various examples of separating the files. I can understand them but the problem comes with the way my code is. I want separate these controllers in different files.
'use strict'; angular.module('myModule', ['ui.bootstrap']); var myApp = angular.module('myApp', []); myApp.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }]); myApp.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/getplaces', { templateUrl: 'templates/getplaces.html', controller: 'ListCtrl' })
The below controller needs to be in a different file.
///////////// MONTHLY DATA ///////////////////////////////////// myApp.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/getmonth', { templateUrl: 'templates/getmacs.html', controller: 'MonthlyCtrl' }) }]) .controller('MonthlyCtrl', function ($scope, $http) { $scope.visible = true; })
I have more than 20 controllers like above. but how do I separate them.
解决方案This should easily be done, I would organize my application route configurations into the main app.js file.
myApp.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/getplaces', { templateUrl: 'templates/getplaces.html', controller: 'ListCtrl' }).when('/getmonth', { templateUrl: 'templates/getmacs.html', controller: 'MonthlyCtrl' }) }])
Then when you create a separate controller in each file just reference the application name as such:
myApp.controller('MonthlyCtrl', ['$scope', '$http', function ($scope, $http) { $scope.visible = true; }])
You will also notice I am using the array initializer way, this will save you some hastles when you are doing minification.
这篇关于如何分离在angularjs Controller文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!