[英]Using *ngFor and retrieving html text from ion-label
I've been having a problem where I use *ngFor like this in my HTML:我在 HTML 中像这样使用 *ngFor 时遇到问题:
<ion-slides #slides [options]="slideOpts">
<ion-slide class="numbers-wrapper"
*ngFor="let questionNumber of numbers"
(click)="clickQuestionNumber()">
<ion-label #quesNum>{{ questionNumber }}</ion-label>
</ion-slide>
</ion-slides>
from an array in my TS file:从我的 TS 文件中的数组:
numbers = [
'01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21',
'22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40'];
It works fine and it displays as I wish, but my problem is that I would like to retrieve on click the text inside the ion-label {{ questionNumber }}它工作正常,它按我的意愿显示,但我的问题是我想在点击 ion-label {{ questionNumber }} 内的文本时检索
I have tried @viewchild
and getElementbyId
but it returns(or prints) the first value which is 01. When I check on the HTML of Developer tools, all the html from 01 to 40 are there as well as it displays on my screen.我已经尝试过
@viewchild
和getElementbyId
,但它返回(或打印)第一个值为 01。当我检查开发人员工具的 HTML 时,从 01 到 40 的所有 html 都在那里,并且它显示在我的屏幕上。 For some reason I can't manage to retrieve all numbers.由于某种原因,我无法检索所有数字。 I would like that whenever I click in 01 it returns 01, and 02 returns 02 and and so on.
我希望每当我点击 01 时它会返回 01,而 02 会返回 02,依此类推。
this is my method:这是我的方法:
clickQuestionNumber() {
this.currentNumber = this.quesNum.nativeElement.innerText;
}
I don't have much now since I've been trying all sort of things but it always return only the first value.我现在没有多少,因为我一直在尝试各种事情,但它总是只返回第一个值。 Thank you
谢谢
You can simply do it on (click)
你可以简单地做到这一点
(click)
<ion-slide class="numbers-wrapper"
*ngFor="let questionNumber of numbers"
(click)="currentNumber = questionNumber">
Or,Try like this:或者,试试这样:
.html .html
<ion-slide class="numbers-wrapper"
*ngFor="let questionNumber of numbers"
(click)="clickQuestionNumber(questionNumber )">
.ts .ts
clickQuestionNumber(number) {
this.currentNumber = number;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.