在AngularJS分离控制器 [英] Separating Controllers in AngularJS

查看:204
本文介绍了在AngularJS分离控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去我的角度控制器分离成不同的文件。现在每个人看起来是这样的。

Im trying to separate my Angular controllers into different files. Right now each one appears like this.

AddPropertyController.js

AddPropertyController.js

angular.module('myApp.controllers.addproperty', [])
    .controller(...);

SearchController

SearchController

angular.module('myApp.controllers.search', [])
.controller(...);

现在对这些我必须包括他们在角对myApp各一个。通过:

Now for each one of these I had to include them in the Angular myApp. via:

angular.module('myApp', [
        'myApp.controllers.search',
        'myApp.controllers.addproperty'
    ])

我的问题是,有没有办法只包括一次全部,而不是逐个添加呢?我的第一次尝试命名每个模块相同,angular.module('myApp.controllers',...),那么只包含在App myApp.controllers。但是,这似乎并没有工作...我想林问的是什么是有分离器的文件的最好方法,但都是一个模块中。感谢您的帮助!

my question is, is there a way to just include them all at once instead of adding them individually? My first attempt was to name each module the same, angular.module('myApp.controllers',...) then just include myApp.controllers in the App. But that didn't seem to work... I guess Im asking is what is the best way to have separated controller files but in all in one module. Thanks for the help!

推荐答案

查看 angularjs文档有关控制器的信息,并把它们添加到应用级模块...

Check out the angularjs docs for info on controllers and adding them to app level modules...

例如您code:

var myApp = angular.module('myApp',[]);

myApp.controller('SearchController', ['$scope', function($scope) {
  $scope.search = 'Hola!';
}]);

myApp.controller('AddPropertiesController', ['$scope', function($scope) {
  $scope.props = [];
}]);

你基本上连接每个控制器到MyApp实例,因此应用程序知道该控制器。

You're basically attaching each controller to the myApp instance, so the app is aware of the controllers.

这篇关于在AngularJS分离控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆