Tox*_*xic 0 angular angular-reactive-forms
我正在开发 Angular 5 应用程序。我有反应形式,一切正常。现在我需要在用户单击复选框 true 时禁用输入。一切正常,希望知道如何禁用组件的输入。我成功获取了输入的 id,但不知道下一步做什么???
private handleQuestionAnswerProvidedStateChange(questionAnswerProvidedState:QuestionAnswerProvidedStateDataModel)
{
console.log(">>>>>>> questionAnswerProvidedStateEvent ",questionAnswerProvidedState);
var elementReference = document.getElementById(questionAnswerProvidedState.QuestionId);
console.log("elementReference ",elementReference);
}
Run Code Online (Sandbox Code Playgroud)
<div *ngSwitchCase="'textbox'"> <small>textbox</small>
<div class="form-group">
<input *ngSwitchCase="'textbox'"
[formControlName]="question.key"
[id]="question.key"
[type]="question.type"
[(ngModel)]="question.value"
(keyup.enter)="onChangeValue($event, previousValue, responseId, question.key, 'textbox'); previousValue = question.value"
(blur)="onChangeValue($event, previousValue, responseId, question.key, 'textbox'); previousValue = question.value"
(focus)="previousValue=question.value"
>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
以下文本是循环,因此会有很多输入,每个输入都有唯一的 id
为输入元素创建模板引用#inputEl并在组件文件中使用它。
<div class="form-group">
<input #inputEl
*ngSwitchCase="'textbox'"
[formControlName]="question.key"
[id]="question.key"
[type]="question.type"
[(ngModel)]="question.value"
(keyup.enter)="onChangeValue($event, previousValue, responseId, question.key, 'textbox'); previousValue = question.value"
(blur)="onChangeValue($event, previousValue, responseId, question.key, 'textbox'); previousValue = question.value"
(focus)="previousValue=question.value"
>
</div>
Run Code Online (Sandbox Code Playgroud)
ViewChild()在组件 .ts 文件中创建一个用于输入元素的属性并设置disabled = true
组件.ts
@ViewChild('inputEl') public inputEl: ElementRef;
fun() {
this.inputEl.nativeElement.disabled = true;
}
Run Code Online (Sandbox Code Playgroud)
编辑 :
由于输入是动态生成的并且正在获取每个元素的 id。
以下内容应该有所帮助:
document.getElementById(questionAnswerProvidedState.QuestionId).setAttribute('disabled', 'true');