如何创建单独AngularJS控制器中的文件? [英] How to create separate AngularJS controller files?

查看:151
本文介绍了如何创建单独AngularJS控制器中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我所有的AngularJS控制器在一个文件中,controllers.js。这个文件的结构如下:

I have all of my AngularJS controllers in one file, controllers.js. This file is structured as follows:

angular.module('myApp.controllers', [])
  .controller('Ctrl1', ['$scope', '$http', function($scope, $http) {    
  }])
  .controller('Ctrl2', ['$scope', '$http', function($scope, $http) }
  }])

我想什么做的就是把CTRL1和CTRL2为独立的文件。然后,我将包括我的index.html这两个文件,​​而是应当如何运用这种的结构?我试图做一些这样的事情,它在Web浏览器控制台抛出一个错误说,它不能找到我的控制器。任何提示?

What I'd like to do is put Ctrl1 and Ctrl2 into separate files. I would then include both files in my index.html, but how should that be structured? I tried doing some thing like this and it throws an error in the web browser console saying it can't find my controllers. Any hints?

我搜索计算器,发现这个类似的问题 - 但是,这种语法使用上的角顶一个不同的框架(CoffeeScript的),所以我一直没能跟随

I searched StackOverflow and found this similar question - however, this syntax is using a different framework (CoffeeScript) on top of Angular, and so I haven't been able to follow.

<一个href=\"http://stackoverflow.com/questions/12655890/angularjs-how-do-i-create-controllers-in-multiple-files\">AngularJS:如何创建多个文件中的控制器

推荐答案

文件之一:

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

文件中的两个:

angular.module('myApp.controllers').controller('Ctrl1', ['$scope', '$http', function($scope, $http){

}]);

文件三:

angular.module('myApp.controllers').controller('Ctrl2', ['$scope', '$http', function($scope, $http){

}]);

包含的顺序。我建议3文件,以便模块的声明是对自己。

Include in that order. I recommend 3 files so the module declaration is on its own.

对于文件夹结构有关于这个问题很多很多很多的意见,但是这两个是pretty好

As for folder structure there are many many many opinions on the subject, but these two are pretty good

https://github.com/angular/angular-seed

<一个href=\"http://briantford.com/blog/huuuuuge-angular-apps.html\">http://briantford.com/blog/huuuuuge-angular-apps.html

这篇关于如何创建单独AngularJS控制器中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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