This command runs the TypeScript compiler in "watch mode", recompiling automatically when the code changes.
The command simultaneously launches the app in a browser and refreshes the browser when the code changes.
在后续构建《英雄指南》过程中,应用能持续运行,而不用中断服务来编译或刷新浏览器。
You can keep building the Tour of Heroes without pausing to recompile or refresh the browser.
The double curly braces are Angular's interpolation binding syntax.
These interpolation bindings present the component's title and hero property values,
as strings, inside the HTML header tags.
To show all of the hero's properties,
add a <div> for the hero's id property and another <div> for the hero's name.
To keep the template readable, place each <div> on its own line.
The backticks around the component template let you put the <h1>, <h2>, and <div> elements on their own lines,
thanks to the template literals feature in ES2015 and TypeScript. For more information, see
Template literals.
Users should be able to edit the hero name in an <input> textbox.
The textbox should both display the hero's name property
and update that property as the user types.
也就是说,我们要在表单元素<input>和组件的hero.name属性之间建立双向绑定。
You need a two-way binding between the <input> form element and the hero.name property.
双向绑定
Two-way binding
把模板中的英雄名字重构成这样:
Refactor the hero name in the template so it looks like this:
[(ngModel)] is the Angular syntax to bind the hero.name property
to the textbox.
Data flows in both directions: from the property to the textbox,
and from the textbox back to the property.
不幸的是,做了这项改动之后,我们的程序崩溃了。
打开浏览器的控制台,我们会看到Angular抱怨说:“ngModel ... isn't a known property of input.”(ngModel不是input元素的已知属性)
Unfortunately, immediately after this change, the application breaks.
If you looked in the browser console, you'd see Angular complaining that
"ngModel ... isn't a known property of input."
Although NgModel is a valid Angular directive, it isn't available by default.
It belongs to the optional FormsModule.
You must opt-in to using that module.
Open the app.module.ts file and import the FormsModule symbol from the @angular/forms library.
Then add the FormsModule to the @NgModule metadata's imports array, which contains the list
of external modules that the app uses.
When the browser refreshes, the app should work again.
You can edit the hero's name and see the changes reflected immediately in the <h2> above the textbox.
The Tour of Heroes app uses the double curly braces of interpolation (a type of one-way data binding)
to display the app title and properties of a Hero object.
我们使用 ES2015 的模板字符串写了一个多行模板,使我们的模板更具可读性。
You wrote a multi-line template using ES2015's template literals to make the template readable.
You added a two-way data binding to the <input> element
using the built-in ngModel directive. This binding both displays the hero's name and allows users to change it.
ngModel指令将这些修改传播到每一个对hero.name的其它绑定。
The ngModel directive propagates changes to every other binding of the hero.name.
In the next tutorial page, you'll build on the Tour of Heroes app to display a list of heroes.
You'll also allow the user to select heroes and display their details.
You'll learn more about how to retrieve lists and bind them to the template.