Angular 应用是由组件组成的。
组件由 HTML 模板和组件类组成,组件类控制视图。下面是一个显示简单字符串的组件:
Angular applications are made up of components.
A component is the combination of an HTML template and a component class that controls a portion of the screen. Here is an example of a component that displays a simple string:
src/app/app.component.ts
import{Component}from'@angular/core';@Component({
selector:'my-app',template:`<h1>Hello {{name}}</h1>`})exportclassAppComponent{ name ='Angular';}
每个组件都以@Component装饰器函数开始,它接受一个元数据对象参数。该元素对象描述了 HTML 模板和组件类是如何一起工作的。
Every component begins with an @Componentdecorator
function that takes a metadata object. The metadata object describes how the HTML template and component class work together.
The template property defines a message inside an <h1> header.
The message starts with "Hello" and ends with {{name}},
which is an Angular interpolation binding expression.
At runtime, Angular replaces {{name}} with the value of the component's name property.
Interpolation binding is one of many Angular features you'll discover in this documentation.
在这个例子中,把组件类的name属性从'Angular'改为'World',看看会怎么样。
In the example, change the component class's name property from 'Angular' to 'World' and see what happens.
This example is written in TypeScript, a superset of JavaScript. Angular
uses TypeScript because its types make it easy to support developer productivity with tooling. You can also write Angular code in JavaScript; this guide explains how.