angular模板与数据绑定

一. 数据显示

1. 使用插值表达式显示组件属性
template: `
  <h1>{
  {title}}</h1>
  <h2>My favorite hero is: {
  {myHero}}</h2>
  `

JavaScript 中那些具有或可能引发副作用的表达式是被禁止的,包括:

  • 赋值 (=, +=, -=, ...)
  • new运算符
  • 使用;或,的链式表达式
  • 自增或自减操作符 (++和--)

模板是包在 ECMAScript 2015 反引号 () 中的一个多行字符串。 反引号 () — 注意,不是单引号 (') — 允许把一个字符串写在多行上, 使 HTML 模板更容易阅读。

注意,我们没有调用 new 来创建AppComponent类的实例,是 Angular 替我们创建了它。

export class AppCtorComponent {
  title: string;
  myHero: string;

  constructor() {
    this.title = 'Tour of Heroes';
    this.myHero = 'Windstorm';
  }
}
2.使用ngFor显示数组属性
template: `
  <h1>{
  {title}}</h1>
  <h2>My favorite hero is: {
  {myHero}}</h2>
  <p>Heroes:</p>
  <ul>
    <li *ngFor="let hero of heroes">
      {
  { hero }}
    </li>
  </ul>
`

ngFor用于显示一个“数组”, 但ngFor可以为任何可迭代的 (iterable) 对象重复渲染条目。

4. 使用NgForOf显示数组属性
  • index: number: The index of the current item in the iterable.
  • count: number:
  • first: boolean: True when the item is the first item in the iterable.
  • last: boolean: True when the item is the last item in the iterable.
  • even: boolean: True when the item has an even index in the iterable.
  • odd: boolean: True when the item has an odd index in the iterable.
<li *ngFor="let user of userObservable | async as users; index as i; first as isFirst">
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值