Javascript 如何基于*ngIf更改按钮状态
在下面的代码中,我想执行以下操作Javascript 如何基于*ngIf更改按钮状态,javascript,angular,typescript,Javascript,Angular,Typescript,在下面的代码中,我想执行以下操作 if index is equal to 0 the text of the button should be info and enabled 及 请看我下面的尝试,让我知道答案 代码: td *ngFor="let application of applications;let index=index"> <p> <button (click)="showInfoWindow(
if index is equal to 0 the text of the button should be info and enabled
及
请看我下面的尝试,让我知道答案
代码:
td *ngFor="let application of applications;let index=index">
<p>
<button (click)="showInfoWindow(application)" class="btn btn-sm btn-primary"
*ngIf = "index == 1; else INFO">info</button>
</p>
<ng-template #INFO disabled>
INFO.
</ng-template>
</td>
td*ngFor=“让应用程序应用;让索引=索引”>
信息
信息。
您可以使用三元运算符为标签应用条件,并使用[disabled]
属性启用/禁用按钮。你可以利用0
在JS中的虚假性
试试下面的方法
if index is equal to 0 the text of the button should be info and enabled
{{!!索引?'INFO':'INFO'}
更新:使用*ngIf
信息
信息
工作示例:
更新:调试当前索引
直接在模板中呈现索引比在console.log中呈现当前索引更容易、更快
当前索引:{index}
...
但是,如果您坚持打印到控制台中,您可以在控制器中定义一个函数,并使用插值将当前索引传递给它
控制器
printIndex(索引:编号){
控制台日志(索引);
}
模板
{{printIndex(index)}}
...
但请注意:这可能会产生极大的误导,因为默认的更改检测策略会在每个更改检测周期触发该函数。这是否意味着
应用程序
数组中只有两个元素?@MichaelD yes。但我想实现我在问题中解释的内容,而不考虑应用程序“不考虑数字”是什么意思?因此,如果index=0,那么启用和'info'以及禁用和'info'用于任何其他索引?我们可以使用*ngIf'实现这一点吗?我的意思是,您发布的代码很有启发性,但要在ngIf@user2121:是的,你可以,但我不知道你为什么要那样做。这只会导致按钮元素的代码重复。我已经更新了答案。您的意思是在这种情况下,我们不需要使用ngIf!!请让我知道你为什么在索引bt之前!!在你发布的代码中,{{!!index?'INFO':'INFO'}}这是什么意思