admin管理员组文章数量:1641439
在html页面中直接按低版本的方式配置[attr.disabled]="true",不生效,而且运行时按F12可以看到如下警告:
It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors.
Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
last: new FormControl('Drew', Validators.required) });
大概意思是在Form组件的ts文件中,构建FormGroup的时候,直接指定即可。代码调整如下:
@Component({
selector: 'app-edituser',
templateUrl: './edituserponent.html',
styleUrls: ['./edituserponent.css']
})
export class EdituserComponent implements OnInit {
validateForm: FormGroup;
constructor(private modal: NzModalRef, private fb: FormBuilder) {
//this.user = new User();
}
//主要修改ngOnInit方法中的this.fb.group函数的传参,UserId组件的disabled属性根据this.IdEditable变量动态计算
ngOnInit() {
console.log("[IdEditable] = " + this.IdEditable)
this.validateForm = this.fb.group({
UserId: new FormControl({value:this.uId, disabled: !this.IdEditable}, Validators.required),
UserName: new FormControl({value:this.uName}, Validators.required)
});
}
@Input() uId: string;
@Input() uName: string;
IsConfirm: boolean;
public IdEditable: boolean = true
private opMode: string;
public btnConfirmClick() {
this.IsConfirm = true;
this.modal.destroy({uId: this.uId, uName: this.uName, opMode: this.opMode});
return false;
}
public btnCancelClick() {
this.IsConfirm = false;
this.modal.destroy({uId: '-2', uName: '', opMode: 'cancel'});
return false;
}
}
组件的html内容中,去除input元素的disabled属性:
<form name="form" nz-form [formGroup]="validateForm">
<nz-form-item>
<nz-form-label [nzSm]="6" [nzXs]="24" nzFor="txtUserId" nzRequired>用户ID</nz-form-label>
<nz-form-control [nzSm]="14" [nzXs]="24">
<input nz-input id="txtUserId" name="txtUserId" formControlName="UserId" [(ngModel)]="uId" placeholder="用户ID">
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label [nzSm]="6" [nzXs]="24" nzFor="txtUserName" nzRequired>用户姓名</nz-form-label>
<nz-form-control [nzSm]="14" [nzXs]="24">
<input nz-input id="txtUserId" formControlName="UserName" [(ngModel)]="uName" placeholder="用户ID">
<nz-form-explain *ngIf="validateForm.get('UserName').dirty&&validateForm.get('UserName').hasError('required')">请输入用户姓名!</nz-form-explain>
</nz-form-control>
</nz-form-item>
<hr/>
<nz-form-item nz-row style="margin-bottom:8px;">
<nz-form-control [nzSpan]="14" [nzOffset]="6" style="display: flex; justify-content: center;">
<button nz-button nzType="primary" (click)="btnConfirmClick()" [disabled]="!validateForm.valid">确定</button>
<button nz-button nzType="primary" (click)="btnCancelClick()">取消</button>
</nz-form-control>
</nz-form-item>
</form>
版权声明:本文标题:Angular7设置Input的disabled属性 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729318732a1195615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论