当我尝试patchValue时,它根本不起作用。也没有错误。
@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>
相同的代码将在我拥有的其他模块中运行,但由于某些原因,它在此组件中将无法运行。
当我登录serviceplanForm时,表单显示正确。
好吧,当我在httpcall中调用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'
});
});