如何根据必填字段和可选字段启用或禁用 [​​=10=] 2 中的任何按钮

How to enable or disable any button in angular 2 based on mandatory and optional field

我希望在占位符值为 'Optional' 时启用我的“保存”按钮。当值为 'Mandatory' 时,“保存”按钮应该被禁用,只有在我为此输入一些值时才会启用场.

我的模板-

<md-input name="" [placeholder]="isOptional()"></md-input>
<input type="button" disabled="placeholder==='Mandatory'" value="Save">

在我的打字稿文件中-

isOptional(){
if (cond1|| cond2){
 return 'Mandatory';
}
else if (cond3||cond4){
return 'Optional';
}

我正在我的模板中尝试类似上面的内容,但没有得到正确的 output.Any 我哪里出错了?

在你的组件中:

option: string;

isOptional(){
if (cond1|| cond2){
 this.option = 'Mandatory';
}
else if (cond3||cond4){
this.option = 'Optional';
}

在html中:

<input type="button" [disabled]="option==='Mandatory'" value="Save">

尝试以下操作:

<md-input name="" #input [placeholder]="isOptional()"></md-input>
<input type="button" disabled="input.placeholder === 'Mandatory'" value="Save">

通常你想制作一个表格。你可以这样做:

<form
    (ngSubmit)="sendFunction(anyForm)"
    #anyForm="ngForm"
>
    <input
        type="text"
        placeholder="any placeholder"
        required
    />
    <button type="submit" [disabled]="!anyForm.form.valid">
        Add Todo
    </button>
</form>