include ../_util-fns .alert.is-important :marked This guide is still in the process of being updated to RC5 and it's samples may not work correctly. :marked Having an existing Angular 1 application doesn't mean that we can't begin enjoying everything Angular 2 has to offer. That's because Angular 2 comes with built-in tools for migrating Angular 1 projects over to the Angular 2 platform. 已经有了一个Angular 1的程序并不表示我们就不能享受Angular 2提供的一切。 这是因为Angular 2带来了一些内建工具,来帮助我们把Angular 1的项目迁移到Angular 2平台。 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 Angular 1 applications with Angular 2 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 Angular 1 applications. 有些应用可能比其它的升级起来简单,还有一些方法能让把这项工作变得更简单。 即使在正式开始升级过程之前,我们可以准备Angular 1的程序,让它向Angular 2看齐。 这些准备步骤几乎都是关于如何让代码更加松耦合、更有可维护性,以及用现代开发工具提高速度的。 这意味着,这种准备工作不仅能让最终的升级变得更简单,而且还能提升Angular 1程序的质量。 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 Angular 1 components to Angular 2 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 2 has been designed to make incremental upgrading seamless. 成功升级的关键之一是增量式的实现它,通过在同一个应用中一起运行这两个框架,并且逐个把Angular 1的组件迁移到Angular 2中。 这意味着可以在不必打断其他业务的前提下,升级更大、更复杂的应用程序,因为这项工作可以多人协作完成,在一段时间内逐渐铺开。 Angular 2 `upgrade`模块的设计目标就是让你渐进、无缝的完成升级。 1. [Preparation](#preparation) 1. [准备工作](#preparation) 1. [Following The Angular Style Guide](#following-the-angular-style-guide) 1. [遵循Angular风格指南](#following-the-angular-style-guide) 2. [Using a Module Loader](#using-a-module-loader) 2. [使用模块加载器](#using-a-module-loader) 3. [Migrating to TypeScript](#migrating-to-typescript) 3. [迁移到TypeScript](#migrating-to-typescript) 4. [Using Component Directives](#using-component-directives) 4. [使用组件型指令](#using-component-directives) 2. [Upgrading with The Upgrade Adapter](#upgrading-with-the-upgrade-adapter) 2. [通过升级适配器进行升级](#upgrading-with-the-upgrade-adapter) 1. [How The Upgrade Adapter Works](#how-the-upgrade-adapter-works) 1. [如何升级适配器](#how-the-upgrade-adapter-works) 2. [Bootstrapping Hybrid Angular 1+2 Applications](#bootstrapping-hybrid-angular-1-2-applications) 2. [引导Angular 1和2的混合(hybrid)应用](#bootstrapping-hybrid-angular-1-2-applications) 3. [Using Angular 2 Components from Angular 1 Code](#using-angular-2-components-from-angular-1-code) 3. [从Angular 1的代码中使用Angular 2的组件](#using-angular-2-components-from-angular-1-code) 4. [Using Angular 1 Component Directives from Angular 2 Code](#using-angular-1-component-directives-from-angular-2-code) 4. [从Angular 2的代码中使用Angular 1的组件](#using-angular-1-component-directives-from-angular-2-code) 5. [Projecting Angular 1 Content into Angular 2 Components](#projecting-angular-1-content-into-angular-2-components) 5. [把Angular 1的内容投影(project)进Angular 2组件中](#projecting-angular-1-content-into-angular-2-components) 6. [Transcluding Angular 2 Content into Angular 1 Component Directives](#transcluding-angular-2-content-into-angular-1-component-directives) 6. [把Angular 2的内容透传(transclude)到Angular 1的组件型指令中](#transcluding-angular-2-content-into-angular-1-component-directives) 7. [Making Angular 1 Dependencies Injectable to Angular 2](#making-angular-1-dependencies-injectable-to-angular-2) 7. [让Angular 1提供的依赖可以被注入到Angular 2](#making-angular-1-dependencies-injectable-to-angular-2) 8. [Making Angular 2 Dependencies Injectable to Angular 1](#making-angular-2-dependencies-injectable-to-angular-1) 8. [让Angular 2提供的依赖可以被注入到Angular 1](#making-angular-2-dependencies-injectable-to-angular-1) 3. [PhoneCat Upgrade Tutorial](#phonecat-upgrade-tutorial) 3. [PhoneCat准备工作教程](#phonecat-preparation-tutorial) 1. [Switching to TypeScript](#switching-to-typescript) 1. [切换到TypeScript](#switching-to-typescript) 2. [Installing Angular 2](#installing-angular-2) 2. [安装Angular 2](#installing-angular-2) 3. [Bootstrapping A Hybrid 1+2 PhoneCat](#bootstrapping-a-hybrid-1-2-phonecat) 3. [引导Angular 1+2的混合版PhoneCat](#bootstrapping-a-hybrid-1-2-phonecat) 4. [Upgrading the Phone service](#upgrading-the-phone-service) 4. [升级Phone服务](#upgrading-the-phone-service) 5. [Upgrading Components](#upgrading-components) 5. [升级组件](#upgrading-components) 6. [Switching To The Angular 2 Router And Bootstrap](#switching-to-the-angular-2-router-and-bootstrap) 6. [切换到Angular 2的路由器并引导](#switching-to-the-angular-2-router-and-bootstrap) 7. [Saying Goodbye to Angular 1](#saying-goodbye-to-angular-1) 7. [再见,Angular 1!](#saying-goodbye-to-angular-1) .l-main-section :marked # Preparation # 准备工作 There are many ways to structure Angular 1 applications. When we begin to upgrade these applications to Angular 2, 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. Angular 1应用程序的组织方式有很多种。当我们想把它们升级到Angular 2的时候, 有些做起来会比其它的更容易些。即使在我们开始升级之前,也有一些关键的技术和模式可以让我们将来升级时更轻松。 ## Following The Angular Style Guide ## 遵循Angular风格指南 The [Angular 1 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 Angular 1 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)收集了一些已证明能写出干净且可维护的Angular 1程序的模式与实践。 它包含了很多关于如何书写和组织Angular代码的有价值信息,同样重要的是,**不应该**采用的书写和组织Angular代码的方式。 Angular 2 is a reimagined version of the best parts of Angular 1. In that sense, its goals are the same as the Angular Style Guide's: To preserve the good parts of Angular 1, and to avoid the bad parts. There's a lot more to Angular 2 than just that of course, but this does mean that *following the style guide helps make your Angular 1 app more closely aligned with Angular 2*. Angular 2是一个基于Angular 1中最好的部分构思出来的版本。在这种意义上,它的目标和Angular风格指南是一样的: 保留Angular 1中好的部分,去掉坏的部分。当然,Angular 2还做了更多。 说这些的意思是:*遵循这个风格指南可以让你写出更接近Angular 2程序的Angular 1程序*。 There are a few rules in particular that will make it much easier to do *an incremental upgrade* using the Angular 2 `upgrade` module: 特别是某些规则会让使用Angular 2的`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*. 当我们把应用代码分解成每个文件中放一个组件之后,我们通常会得到一个由大量相对较小的文件组成的项目结构。 这比组织成少量大文件要整洁得多,但如果你不得不通过`