作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用模板驱动的表单,并尝试根据某些对象属性设置表单元素的值,但是我面临以下问题 这样做。另外,这是我的 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/
我是一名优秀的程序员,十分优秀!