@title 从 AngularJS 升级 @intro AngularJS 应用可以逐步升级到 Angular。 @description _Angular_ is the name for the Angular of today and tomorrow. _AngularJS_ is the name for all v1.x versions of Angular. *Angular*这个名字专指现在和未来的Angular版本,而*AngularJS*专指Angular的所有v1.x版本。 AngularJS apps are great. Always consider the business case before moving to Angular. An important part of that case is the time and effort to get there. This guide describes the built-in tools for efficiently migrating AngularJS projects over to the Angular platform, a piece at a time. 有很多大型AngularJS应用。 在决定迁移到Angular之前,首先要深入思考业务案例。 在这些案例中,最重要的部分之一是时间和需要付出的努力。 本章描述用于把AngularJS应用高效迁移到Angular平台的内置工具,每次讲一点点。 Some applications will be easier to upgrade than others, and there are ways in which we can make it easier for ourselves. It is possible to prepare and align AngularJS applications with Angular even before beginning the upgrade process. These preparation steps are all about making the code more decoupled, more maintainable, and up to speed with modern development tools. That means the preparation work will not only make the eventual upgrade easier, but will also generally improve our AngularJS applications. 有些应用可能比其它的升级起来简单,还有一些方法能让把这项工作变得更简单。 即使在正式开始升级过程之前,我们可以准备AngularJS的程序,让它向Angular看齐。 这些准备步骤几乎都是关于如何让代码更加松耦合、更有可维护性,以及用现代开发工具提高速度的。 这意味着,这种准备工作不仅能让最终的升级变得更简单,而且还能提升AngularJS程序的质量。 One of the keys to a successful upgrade is to do it incrementally, by running the two frameworks side by side in the same application, and porting AngularJS components to Angular one by one. This makes it possible to upgrade even large and complex applications without disrupting other business, because the work can be done collaboratively and spread over a period of time. The `upgrade` module in Angular has been designed to make incremental upgrading seamless. 成功升级的关键之一是增量式的实现它,通过在同一个应用中一起运行这两个框架,并且逐个把AngularJS的组件迁移到Angular中。 这意味着可以在不必打断其它业务的前提下,升级更大、更复杂的应用程序,因为这项工作可以多人协作完成,在一段时间内逐渐铺开。 Angular `upgrade`模块的设计目标就是让你渐进、无缝的完成升级。 1. [Preparation](guide/upgrade#preparation) [准备工作](guide/upgrade#preparation) 1. [Follow the Angular Style Guide](guide/upgrade#follow-the-angular-style-guide) [遵循Angular风格指南](guide/upgrade#follow-the-angular-style-guide) 2. [Using a Module Loader](guide/upgrade#using-a-module-loader) [使用模块加载器](guide/upgrade#using-a-module-loader) 3. [Migrating to TypeScript](guide/upgrade#migrating-to-typescript) [迁移到TypeScript](guide/upgrade#migrating-to-typescript) 4. [Using Component Directives](guide/upgrade#using-component-directives) [使用组件型指令](guide/upgrade#using-component-directives) 2. [Upgrading with The Upgrade Module](guide/upgrade#upgrading-with-the-upgrade-module) [通过升级模块进行升级](guide/upgrade#upgrading-with-the-upgrade-module) 1. [How The Upgrade Module Works](guide/upgrade#how-the-upgrade-module-works) [升级模块是如何工作的](guide/upgrade#how-the-upgrade-module-works) 2. [Bootstrapping hybrid](guide/upgrade#bootstrapping-hybrid-applications) [引导混合(hybrid)应用](guide/upgrade#bootstrapping-hybrid-applications) 3. [Using Angular Components from AngularJS Code](guide/upgrade#using-angular-components-from-angularjs-code) [从AngularJS的代码中使用Angular的组件](guide/upgrade#using-angular-components-from-angularjs-code) 4. [Using AngularJS Component Directives from Angular Code](guide/upgrade#using-angularjs-component-directives-from-angular-code) [从Angular的代码中使用AngularJS的组件](guide/upgrade#using-angularjs-component-directives-from-angular-components-from-angularjs-code) 5. [Projecting AngularJS Content into Angular Components](guide/upgrade#projecting-angularjs-content-into-angular-components) [把AngularJS的内容投影(project)进Angular组件中](guide/upgrade#projecting-angularjs-content-into-angular-components) 6. [Transcluding Angular Content into AngularJS Component Directives](guide/upgrade#transcluding-angular-content-into-angularjs-component-directives) [把Angular的内容透传(transclude)到AngularJS的组件型指令中](guide/upgrade#transcluding-angular-content-into-angularjs-component-directives) 7. [Making AngularJS Dependencies Injectable to Angular](guide/upgrade#making-angularjs-dependencies-injectable-to-angular) [让AngularJS提供的依赖可以被注入到Angular](guide/upgrade#making-angularjs-dependencies-injectable-to-angular) 8. [Making Angular Dependencies Injectable to AngularJS](guide/upgrade#making-angular-dependencies-injectable-to-angularjs) [让Angular提供的依赖可以被注入到AngularJS](guide/upgrade#making-angular-dependencies-injectable-to-angularjs) 9. [Using Ahead-of-time compilation with hybrid apps](guide/upgrade#using-ahead-of-time-compilation-with-hybrid-apps) [在混合式应用中使用预编译](guide/upgrade#using-ahead-of-time-compilation-with-hybrid-apps) 10. [Dividing routes between Angular and AngularJS](guide/upgrade#dividing-routes-between-angular-and-angularjs) [在Angular和AngularJS之间进行路由](guide/upgrade#dividing-routes-between-angular-and-angularjs) 3. [PhoneCat Upgrade Tutorial](guide/upgrade#phonecat-upgrade-tutorial) [PhoneCat准备工作教程](guide/upgrade#phonecat-preparation-tutorial) 1. [Switching to TypeScript](guide/upgrade#switching-to-typescript) [切换到TypeScript](guide/upgrade#switching-to-typescript) 2. [Installing Angular](guide/upgrade#installing-angular) [安装Angular](guide/upgrade#installing-angular) 3. [Bootstrapping a hybrid PhoneCat](guide/upgrade#bootstrapping-a-hybrid-phonecat) [引导混合版PhoneCat](guide/upgrade#bootstrapping-a-hybrid-phonecat) 4. [Upgrading the Phone service](guide/upgrade#upgrading-the-phone-service) [升级Phone服务](guide/upgrade#upgrading-the-phone-service) 5. [Upgrading Components](guide/upgrade#upgrading-components) [升级组件](guide/upgrade#upgrading-components) 6. [AoT compile the hybrid app](guide/upgrade#aot-compile-the-hybrid-app) [对混合式应用进行AoT预编译](guide/upgrade#aot-compile-the-hybrid-app) 7. [Adding The Angular Router And Bootstrap](guide/upgrade#adding-the-angular-router-and-bootstrap) [添加Angular路由器并引导](guide/upgrade#adding-the-angular-router-and-bootstrap) 8. [Say Goodbye to AngularJS](guide/upgrade#say-goodbye-to-angularjs) [和AngularJS说再见](guide/upgrade#say-goodbye-to-angularjs) 3. [Appendix: Upgrading PhoneCat Tests](guide/upgrade#appendix-upgrading-phonecat-tests) [附录:升级PhoneCat测试](guide/upgrade#appendix-upgrading-phonecat-tests) ## Preparation ## 准备工作 There are many ways to structure AngularJS applications. When we begin to upgrade these applications to Angular, some will turn out to be much more easy to work with than others. There are a few key techniques and patterns that we can apply to future proof our apps even before we begin the migration. AngularJS应用程序的组织方式有很多种。当我们想把它们升级到Angular的时候, 有些做起来会比其它的更容易些。即使在我们开始升级之前,也有一些关键的技术和模式可以让我们将来升级时更轻松。 ### Follow the Angular Style Guide ### 遵循Angular风格指南 The [AngularJS Style Guide](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#single-responsibility) collects patterns and practices that have been proven to result in cleaner and more maintainable AngularJS applications. It contains a wealth of information about how to write and organize Angular code - and equally importantly - how **not** to write and organize Angular code. [Angular风格指南](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#single-responsibility)收集了一些已证明能写出干净且可维护的AngularJS程序的模式与实践。 它包含了很多关于如何书写和组织Angular代码的有价值信息,同样重要的是,**不应该**采用的书写和组织Angular代码的方式。 Angular is a reimagined version of the best parts of AngularJS. In that sense, its goals are the same as the Angular Style Guide's: To preserve the good parts of AngularJS, and to avoid the bad parts. There's a lot more to Angular than just that of course, but this does mean that *following the style guide helps make your AngularJS app more closely aligned with Angular*. Angular是一个基于AngularJS中最好的部分构思出来的版本。在这种意义上,它的目标和Angular风格指南是一样的: 保留AngularJS中好的部分,去掉坏的部分。当然,Angular还做了更多。 说这些的意思是:*遵循这个风格指南可以让你写出更接近Angular程序的AngularJS程序*。 There are a few rules in particular that will make it much easier to do *an incremental upgrade* using the Angular `upgrade` module: 有一些特别的规则可以让使用Angular的`upgrade`模块进行*增量升级*变得更简单: * The [Rule of 1](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#single-responsibility) states that there should be one component per file. This not only makes components easy to navigate and find, but will also allow us to migrate them between languages and frameworks one at a time. In this example application, each controller, component, service, and filter is in its own source file. [单一规则](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#single-responsibility) 规定每个文件应该只放一个组件。这不仅让组件更容易浏览和查找,而且还将允许我们逐个迁移它们的语言和框架。 在这个范例程序中,每个控制器、工厂和过滤器都在它自己的源文件中。 * The [Folders-by-Feature Structure](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#folders-by-feature-structure) and [Modularity](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#modularity) rules define similar principles on a higher level of abstraction: Different parts of the application should reside in different directories and Angular modules. [按特性分目录的结构](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#folders-by-feature-structure)和[模块化](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#modularity)规则在较高的抽象层定义了一些相似的原则:应用程序中的不同部分应该被分到不同的目录和Angular模块中。 When an application is laid out feature per feature in this way, it can also be migrated one feature at a time. For applications that don't already look like this, applying the rules in the Angular style guide is a highly recommended preparation step. And this is not just for the sake of the upgrade - it is just solid advice in general! 如果应用程序能用这种方式把每个特性分到一个独立目录中,它也就能每次迁移一个特性。 对于那些还没有这么做的程序,强烈建议把应用这条规则作为准备步骤。而且这也不仅仅对升级有价值, 它还是一个通用的规则,可以让你的程序更“坚实”。 ### Using a Module Loader ### 使用模块加载器 When we break application code down into one component per file, we often end up with a project structure with a large number of relatively small files. This is a much neater way to organize things than a small number of large files, but it doesn't work that well if you have to load all those files to the HTML page with <script> tags. Especially when you also have to maintain those tags in the correct order. That's why it's a good idea to start using a *module loader*. 当我们把应用代码分解成每个文件中放一个组件之后,我们通常会得到一个由大量相对较小的文件组成的项目结构。 这比组织成少量大文件要整洁得多,但如果你不得不通过`