gpt4 book ai didi

angular - 错误 : There are no form controls registered with this group yet. 如果您使用 ngModel,您可能需要检查下一个刻度(例如使用 setTimeout)

转载 作者:行者123 更新时间:2023-12-02 12:53:16 24 4
gpt4 key购买 nike

我正在使用模板驱动的表单,并尝试根据某些对象属性设置表单元素的值,但是我面临以下问题 Error这样做。另外,这是我的 TD 表单代码以及我如何尝试访问它。

.html 文件:

<div class="row">
<div class="col-xs-12">
<form (ngSubmit)="onAddDevice(f)" #f="ngForm">
<div class="row">
<div class="col-sm-5 form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" ngModel required>
</div>
<div class="col-sm-2 form-group">
<label for="type">Type</label>
<input type="text" id="type" class="form-control" name="type" ngModel required>
</div>
<div class="col-sm-5 form-group">
<label for="platform">Platform</label>
<input type="text" id="platform" class="form-control" name="platform" ngModel required>
</div>
<div class="col-sm-2 form-group">
<label for="udid">UDID</label>
<input type="text" id="udid" class="form-control" name="udid" ngModel required>
</div>
</div>

<div class="row">
<div class="col-xs-12">
<button class="btn btn-success" type="submit" [disabled]="!f.valid">Add</button>
<button class="btn btn-danger" type="button">Delete</button>
<button class="btn btn-primary" type="button">Clear</button>
</div>
</div>
</form>
</div>
</div>

.ts 文件:

  @ViewChild('f') deviceForm: NgForm;
@Output() deviceAdded = new EventEmitter<DeviceModel>();
editedItemIndex: number;
editedDevice: DeviceModel;

constructor(private deviceService: DeviceService, private route: ActivatedRoute) { }

ngOnInit() {
this.route.params
.subscribe(
(params: Params) =>{
this.id = +params['id'];
this.editMode = params['id'] != null;
if ( this.editMode) {
this.editedItemIndex = params['id'];
this.editedDevice = this.deviceService.getDevice(params['id']);
console.log(this.editedDevice.frame);
this.deviceForm.setValue({
name: this.editedDevice.name,
platform: this.editedDevice.platform,
type: this.editedDevice.type,
udid: this.editedDevice.frame
});
}
}
);
}

问题是由于调用表单上的 setValue 方法造成的。

最佳答案

我遇到了同样的问题,我通过这样做解决了它(下面的代码片段,使用您的表单控件):

this.deviceForm.form.patchValue({
name: this.editedDevice.name,
platform: this.editedDevice.platform,
type: this.editedDevice.type,
udid: this.editedDevice.frame
});

关于angular - 错误 : There are no form controls registered with this group yet. 如果您使用 ngModel,您可能需要检查下一个刻度(例如使用 setTimeout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com