最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Angular 2-NgFor使用數(shù)字代替集合

Angular 2-NgFor使用數(shù)字代替集合

收到一只叮咚 2019-10-28 10:45:00
...例如...<div class="month" *ngFor="#item of myCollection; #i = index">...</div>可以做類似...<div class="month" *ngFor="#item of 10; #i = index">...</div>...沒(méi)有吸引力的解決方案,例如:<div class="month" *ngFor="#item of ['dummy','dummy','dummy','dummy','dummy','dummy','dummy','dummy']; #i = index">...</div>?
查看完整描述

3 回答

?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

在組件內(nèi),您可以定義一個(gè)數(shù)字?jǐn)?shù)組(ES6),如下所述:


export class SampleComponent {

  constructor() {

    this.numbers = Array(5).fill().map((x,i)=>i); // [0,1,2,3,4]

    this.numbers = Array(5).fill(4); // [4,4,4,4,4]

  }

}

請(qǐng)參閱此鏈接以獲取數(shù)組創(chuàng)建:在JavaScript中從1..20創(chuàng)建整數(shù)數(shù)組的最有趣的方法。


然后可以使用以下方法遍歷此數(shù)組ngFor:


@Component({

  template: `

    <ul>

      <li *ngFor="let number of numbers">{{number}}</li>

    </ul>

  `

})

export class SampleComponent {

  (...)

}

或不久:


@Component({

  template: `

    <ul>

      <li *ngFor="let number of [0,1,2,3,4]">{{number}}</li>

    </ul>

  `

})

export class SampleComponent {

  (...)

}


查看完整回答
反對(duì) 回復(fù) 2019-10-28
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

您與“非優(yōu)雅”解決方案非常接近。


怎么樣:


<div class="month" *ngFor="let item of [].constructor(10); let i = index">

...

</div>


在這里,我Array從一個(gè)空數(shù)組獲取構(gòu)造函數(shù):[].constructor,因?yàn)锳rray在模板語(yǔ)法中它不是公認(rèn)的符號(hào),而且我也懶得去做,Array=Array或者counter = Array像@ pardeep-jain在他的第四個(gè)示例中那樣在組件打字稿中也懶得做。我new之所以這么稱呼它,是因?yàn)閚ew不需要從Array構(gòu)造函數(shù)中獲取數(shù)組。


Array(30)和new Array(30)等價(jià)。


該數(shù)組將為空,但這沒(méi)關(guān)系,因?yàn)槟娴闹幌朐谘h(huán)中使用ifrom ;let i = index。


查看完整回答
反對(duì) 回復(fù) 2019-10-28
  • 3 回答
  • 0 關(guān)注
  • 2031 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)