我正在编写 angular2 应用程序。 我需要我所有的按钮并选择禁用,然后我需要启用它们。我想通过使用我的类字段 butDisabled 但没有运气来做到这一点。 我有这段代码:
@Component({
selector: 'my-app',
template:`
<h1>Disable Enable</h1>
<h2> this is a disabled select </h2>
<select type="number" [(ngModel)]="levelNum" (ngModelChange)="toNumber()" disabled>
<option *ngFor="let level of levels" [ngValue]="level.num">{{level.name}}</option>
</select>
<h2> this is a disabled button </h2>
<button type="button" class="btn btn-default {{butDisabled}}">Disabled</button>
<h2> Why can't I disable the select the same way? </h2>
<select type="number" [(ngModel)]="levelNum" (ngModelChange)="toNumber()" class="{{butDisabled}}">
<option *ngFor="let level of levels" [ngValue]="level.num">{{level.name}}</option>
</select>
`,
})
export class AppComponent {
butDisabled = "disabled";
levelNum:number;
levels:Array<Object> = [
{num: 0, name: "AA"},
{num: 1, name: "BB"}
];
}
我不明白为什么我不能使用 {{butDisabled}} 来禁用选择。 我究竟做错了什么? 非常感谢
这是一个plunker
最佳答案
对于您的情况,使用标记 [disabled]="butDisabled"
就足够了,但值得注意的是,此方法不适用于响应式(Reactive)表单,在代码中禁用/启用控件的位置:
this.form.get('myFormControlName').enable();
this.form.get('myFormControlName').disable();
关于drop-down-menu - 禁用angular2中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377103/