Open greper opened 8 years ago
你这个是html的问题吧,input标签的disabled属性是不用赋值的,只要有disabled就是禁用状态。可以用js动态设置属性值,或者写一个style选择器样式
.disabled{ disabled: disabled; }需要时addClass(disabled)
这个你可以直接通过js setAttribute控制 写了个小demo,
/**
* Created by Farris Zhang on 2016/4/25.
*/
import {Component} from "angular2/core";
@Component({
selector: "my-app",
template: `
<input class="input" type="text" placeholder="input some text..." (keyup.enter)="onKey(input.value)" #input>
<p>input:{{inputValue}}</p>
`
})
export class AppComponent {
inputValue:any;
constructor() {
}
onKey(value:any) {
this.inputValue = value;
document.querySelector(".input").setAttribute("disabled","disabled")
}
}
直接在input标签上设disabled
标签可以啊。
<input [disabled]="enable">
然后enable
是一个变量,可以在js中控制变量值true
,false
就可以了
这样也是可以的
import {Component} from "angular2/core";
@Component({
selector: "my-app",
template: `
<input type="text" [disabled]="disabled" (keyup.enter)="onKey(input.value)" #input>
<p>{{inputValue}}</p>
`
})
export class AppComponent {
inputValue:any;
disabled:boolean;
constructor() {
this.disabled = false;
}
onKey(value:any) {
this.inputValue = value;
this.disabled = !this.disabled;
}
}
disabled这个属性,不管值是多少,只要有这个属性就是禁用状态 所以 如果是这样
<input [disabled]="xxx">
就一直是disable状态如果是这样
<input {{xxx?'disabled':''}} />
就会报错 说找不到{{属性求如何简单的设置disabled