Blo*_*mer 6 variables select input angular
我有个问题。我正在使用 Angular 2。我在 html 中有一个下拉选择,在 TS 文件中有一个变量“selectedVariable”。我想在选择选项时更改 TS 文件中的变量。
例如:当某人选择:“SCRYPT”时,变量“selectedAlgorithm”将更新为值“SCRYPT”。
我是一个角度初学者,谢谢您的帮助。
HTML:
<select class="form-control" id="allocation-algorithm">
<option value="SHA-256">SHA-256</option>
<option value="SCRYPT">SCRYPT</option>
<option value="ETHASH">ETHASH</option>
<option value="CRYPTONIGH">CRYPTONIGHT</option>
<option value="X11">X11</option>
</select>
Run Code Online (Sandbox Code Playgroud)
TS:
import {Component} from '@angular/core';
import {HashrateAllocationService} from './hashrateAllocation.service';
@Component({
selector: 'hashrate-allocation',
templateUrl: './hashrateAllocation.html',
styleUrls: ['./hashrateAllocation.scss']
})
export class HashrateAllocation {
selectedAlgorithm = "SHA-256";
allocationTableData:Array<any>;
constructor(private _hashrateTableService: HashrateAllocationService) {
this.allocationTableData = _hashrateTableService.allocationTableData;
};
}
Run Code Online (Sandbox Code Playgroud)
使用双向绑定 [(ngModel)]="selectedAlgorithm"
Angular 中的双向绑定是模型和视图之间的同步。当模型中的数据发生变化时,视图会反映变化,当视图中的数据发生变化时,模型也会更新
超文本标记语言
<select class="form-control" id="allocation-algorithm" [(ngModel)]="selectedAlgorithm">
<option value="SHA-256">SHA-256</option>
<option value="SCRYPT">SCRYPT</option>
<option value="ETHASH">ETHASH</option>
<option value="CRYPTONIGH">CRYPTONIGHT</option>
<option value="X11">X11</option>
</select>
Run Code Online (Sandbox Code Playgroud)
成分
import {Component} from '@angular/core';
import {HashrateAllocationService} from './hashrateAllocation.service';
@Component({
selector: 'hashrate-allocation',
templateUrl: './hashrateAllocation.html',
styleUrls: ['./hashrateAllocation.scss']
})
export class HashrateAllocation {
public selectedAlgorithm = "SHA-256";
allocationTableData:Array<any>;
constructor(private _hashrateTableService: HashrateAllocationService) {
this.allocationTableData = _hashrateTableService.allocationTableData;
};
}
Run Code Online (Sandbox Code Playgroud)