简体   繁体   中英

How to conditionally set input tag's max value base on a value of a Angular component's variable

I have 3-way input control is defined as following

<input type="range" min="0" max="2" formControlName="dhwOption" class="form-control-range" style="width:180px" id="myonoffswitch2">

I would like to dynamically set the range from 2 to 1 using a component class variable's value, it does not work

 <input type="range" min="0" max= {*ngIf="appEngineMsg && appEngineMsg.Type === 0"}?"1" :"2" formControlName="dhwOption" class="form-control-range"

Remove the braces and quotes and try the following code, assuming appEngineMsg is a variable in the .ts

<input type="range" min="0" max="appEngineMsg && appEngineMsg.Type === 0 ? 1 : 2" formControlName="dhwOption" class="form-control-range"

You should use property binding syntax and take advantage of the safe navigation operator :

[max]="appEngineMsg?.Type === 0 ? 1 : 2"

This is a working solution for my own project

<input type="range" min="0" max='{{appEngineMsg && appEngineMsg.Type === 0 ? 1 : 2}}'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM