Angular PATCH值6不工作
当我尝试patchValue时,它根本不起作用。也没有错误Angular PATCH值6不工作,angular,forms,angular6,Angular,Forms,Angular6,当我尝试patchValue时,它根本不起作用。也没有错误 @ViewChild('sForm') serviceplanForm: NgForm; objectid = ''; allisloaded = true; edit = false; constructor(private service: GeneralService, private route: ActivatedRoute, private router: Router) { }
@ViewChild('sForm') serviceplanForm: NgForm;
objectid = '';
allisloaded = true;
edit = false;
constructor(private service: GeneralService,
private route: ActivatedRoute,
private router: Router) { }
ngOnInit() {
this.objectid = this.route.snapshot.params['id'];
this.route.queryParams.subscribe(params => {
if (Object.keys(params).length !== 0 && params.constructor === Object) {
this.edit = true;
this.allisloaded = true;
this.setFields(params);
}
});
}
private setFields(params: any) {
this.serviceplanForm.form.patchValue({
objectname: 'asdfsdf'
});
}
html
<form (ngSubmit)="onSubmit(sForm)" #sForm="ngForm">
<div class="form-group" [ngClass]="{'has-error': (!objectname.valid && sForm.submitted)}">
<label>{{ 'serviceplan.NAME' | translate }} <span class="text-danger">*</span></label>
<input type="text" class="form-control" ngModel #objectname="ngModel" name="objectname" required>
<span *ngIf="!objectname.valid && sForm.submitted" class="help-block text-danger">{{ 'serviceplane.SERVICEPLANNAMEREQUIRED' | translate }}</span>
</div>
</form>
您是否尝试在表单上调用updateValueAndValidity方法?是,但没有更改检查您是否正在使用控制台日志通过
设置字段
方法。@trichetriche我如何标记它?您喜欢这样的serviceplanForm:NgForm
,它是模板驱动的表单,但是patchValue()
是用于被动表单
this.service.callService(new
Method(environment.api.methods.objects.getOne + this.objectid , null, 'get')).subscribe(response => {
console.log(response);
this.serviceplanForm.form.patchValue({
objectname: 'testname'
});
});