创建 AppComponent 类的实例

当你通过 main.ts 中的 AppComponent 类启动时,Angular 在 index.html 中查找一个 元素, 然后实例化一个 AppComponent,并将其渲染到 标签中。

装饰器

  • 装饰器是用来标记的,标记为一个组件、服务……
  • 接受元数据对象

@Component()
@NgModule()
@Injectable()
@Input

管道

管道 是格式化字符串、金额、日期和其它显示数据的好办法。 Angular 发布了一些内置管道,而且你还可以创建自己的管道。

  1. // async 管道
  2. <div class="shipping-item" *ngFor="let shipping of shippingCosts | async">
  3. <span>{{ shipping.type }}</span>
  4. <span>{{ shipping.price | currency }}</span>
  5. </div>
  1. // 管道还能接收一些参数,来控制它该如何进行转换。
  2. <p>The date is {{today | date:'fullDate'}}</p>

元数据(metadata)

  • 有些元数据位于 _@_[_Component_](https://angular.cn/api/core/Component) 装饰器中,你会把它加到组件类上。
  • 另一些关键性的元数据位于 _@NgModule_ 装饰器中。

    顶级类 AppModule

    公共属性(public)

    Angular 只会绑定到组件的公共属性。
    这个 messageService 属性必须是公共属性,因为你将会在模板中绑定到它。
    1. export class MessagesComponents implements OnInit {
    2. constructor(public messageService: MessageService) {}
    3. }