《AngularJS 2权威教程》堪称AngularJS 2领域的里程碑式著作，涵盖了关于AngularJS 2的几乎所有内容。
Turing: Recently, Turing Book Company is translating your co-authored book ng-book 2. Turing Interview feels obliged and honored to have a talk with Nate. What does Angular mean to you so that you want to write a book for it? Or what are the advantages of Angular impressing you, authors, overwhelmingly?
Nate: Angular provides a fantastic framework for writing and organizing your web apps. Component-based web development has shown itself to be a scalable, comprehensible way to write large, maintainable apps. In the past we would manually read and mutate the DOM using a library such as jQuery, and that kind of code, as many of us know, can become very unwieldy. On the other hand, breaking your code down into components breaks the system down so that each component is concerned with a particular part of the view and so the whole app is easier to reason about.
Turing: Is ng-book 2 an upgrade to ng-book 1?
Nate: No - it's a completely new book. Angular 2 and Angular 1 share a common thought-ancestry but the frameworks are pretty different. The Angular 2 book takes not only the new library, but everything we've learned over the years about web development into a completely new book.
Turing: How is the collaboration between your four authors when writing ng-book 2 ? Which parts are in your charge?
Nate: I've mainly been in charge of outlining, researching, editing and coordinating the whole project and I ended up writing a little less than half of the chapters. Felipe Coury also contributed a huge amount of the content and he's done a fantastic job. The main challenge when working with so many authors is unifying the voice, but thankfully we all share a common vision and so that hasn't been much of an issue in practice.
Turing: What do you think of the relation between code and literature?
Nate: Ideally your code is clear enough that it wouldn't need any supporting documentation. Even though we say that code should be written for humans to understand before machines, in practice technological constraints often dictate ordering of code (and mixing of concerns) that aren't optimal for someone learning a codebase for the first time.
There have been lots of takes on addressing this issue, particularly in the area of "literate programming". I think IPython/Jupyter is one of the most mature tools in this space. However, I think literate programming tools often require all code to be present and presented in a linear order. This often means that the examples you can use with them end up being trivial apps and you can't really learn large structures from them. Code is more of a graph than a document and so you need a way to hop around.
One of my latest attempts on this problem is a tool we call
cq. The idea is that you can extract snippets of code using CSS-style selectors to pull them into your documentation. We use this for all of our books and find that it really helps cut down on typos and maintenance (because we always load our code blocks from runnable code on disk). Because the selectors are aware of the code structure it has the advantage of being robust in the face of line number changes.
It's certainly not the last word in mixing writing and code, but it's helped us a lot.
Turing: According to your profile, we know that you've been working with large-scale data since 2009. In this sense, you could give some valuable advice on data problems proposed by readers.
There are situations where we will work with data within its parent scope. Then how to implement the communication between a scope and its parent scope? Is
$broadcast/$emit/$on the best way?
Nate: When you mention $broadcast/$emit/$on, I assume you're referring to Angular 1 apps. ng-book 2 is primarily about Angular 2 which doesn't have these directly, but the concepts below still apply.
In "classic" Angular
$broadcast/$emit was definitely the first tool of choice when passing data between scopes, but my thinking and the tools have evolved a lot since then. This problem is really a specific case of deciding the data-architecture of your app generally. Three other options come to mind:
Instead of using $broadcast/$emit use a service which has an EventEmitter and manage your state through that service
If you like Observables, instead of EventEmitter, have your service hold RxJS streams and let your components subscribe to changes on those streams
Get rid of $scope altogether and use Redux.
Redux is one of the most interesting data architecture patterns to emerge this year. The idea is that you have all of your data in a central data structure which is never mutated directly. Instead you dispatch "actions" and then you pass your old state and the action to a function which returns a new state (the term for this function is a reducer). I'd recommend ditching $emit/$broadcast and $scope altogether and using a Redux store for state for any brand-new Angular projects.
Turing: When dealing with large-scale data,
ng-repeat gets stuck somewhere occasionally. What's your advice on its better performance?
Nate: A good idea here is to show only a limited number and then recycle DOM elements (instead of showing the whole list and creating new elements for each one).
Say you're repeating a list of
divs that are in a scrollable container. You can limit the maximum number of
divs in the DOM to, say, a pool of 100 divs. Then listen to the scrolling on the container element and populate the
divs as appropriate. It takes a bit more work to set this up, but you'll get much better performance.
Turing: Now, let's forecsat the future of AngularJS. Is it possible for AngularJS to cover back-end development and become a back-end solution like Django in the future?
Nate: To my knowledge, becoming a backend framework like Django or Rails isn't a stated goal of the Angular project. Angular is a strong front-end framework that works in the browser on the DOM and is able to make HTTP requests to any APIs it needs to populate data.
That said, Angular 2 has been built such that it will support multiple platforms, most notably mobile development. Being able to create mobile clients in Angular 2 with NativeScript or Ionic is really exciting.
On the server-side, Patrick Stapleton and a group of contributors have done some great work on Angular Universal which is a way to have your server run and ‘boot up’ your app which, in theory, should result in better performance for the client.
Turing: Do you think that AngularJS will be encapsulated into Browsers one day?
It's hard to say but probably not as such. If anything it would be the other way around: the browser's functionalities will grow to make some of the parts of Angular unnecessary. Web Components is a good example of something browsers may eventually support more broadly that could make
Another part browsers might provide are change detection like Zones - we almost saw browser implementations of Object.observe, which was a lot like Angular's
$watch. But this didn't end up becoming part of the ECMA standard, which is probably fine.
But Angular 2 has a lot of other functionality beyond components like routing, dependency injection, testing frameworks, etc. These frameworks are great, but like any framework they have opinions that may not fit every situation and so I don't know that they will ever be integrated into the browser.
Turing: AngularJS is powerful but complex. Could you give some learning suggestions for beginners?
Nate: The best way to learn any programming framework is to build something with it. So my first learning suggestion to beginners is to go to the official documentation website and try to follow the walkthrough. Both Angular 1 and Angular 2 have walkthrough tutorials on their site.
The next thing I'd recommend to beginners is a resource that walks through step-by-step with lots of sample code. I'm biased, but I think ng-book 1 and ng-book 2 are the best places to get that. Egghead.io also has some great video courses.