【发布时间】:2017-10-28 03:16:42
【问题描述】:
我在 Angular 2 应用程序中使用 select2(https://github.com/NejcZdovc/ng2-select2.)。该表单基于反应式/基于模型的表单。 我想知道如何为选择选项激活它的不同状态(脏、触摸、原始)。当我更改选择选项值时,我面临的问题是在我的编辑表单中,它不会使表单变脏或被触动。请帮忙!!
<form id="validation-form" class="form-horizontal form-label-left parsleyjs" data-parsley-priority-enabled="false" novalidate
(ngSubmit)="saveProduct()" [formGroup]="productForm">
<div class="form-group row">
<label for="shelflife" class="col-md-4 col-form-label">Shelf Life (Month) <span class="required">*</span>
<div *ngIf="productForm.get('shelflife').errors">
<ul class="parsley-errors-list filled" id="parsley-id-5"><li class="parsley-required">This value is required</li></ul>
</div>
</label>
<div class="col-md-7">
<select2 class="select2-selection select2-selection--single" [data]="shelflife_data | async" [value]="selectedShelflife | async" [width]="'100%'"
(valueChanged)="onShelflifeChanged($event)" [options]="{theme: 'bootstrap'}"></select2>
</div>
</div>
下面是组件ts代码
ngOnInit(): void {
jQuery('.parsleyjs').parsley({
errorsContainer: function (elem, isRadioOrCheckbox) {
return jQuery(elem.$element).closest('.form-group').children('label');
}
});
this.productForm = this.fb.group({
gtin: [''],
fdoname_en: [''],
fdoname_fa: [''],
newirc: [''],
singleunitpackage: [''],
multiunitpackage: [''],
shelflife: ['', [Validators.required]],
dosageform: ['', [Validators.required]],
routeadmin: [''],
productcategory: [''],
status: [''],
genericname_en:['', [Validators.required]]
});
this.loadShelflife();
this.loadGenericProduct()
this.loadDosageForm();
this.loadProductCat();
// Read the gtin from the route parameter
this.sub = this.route.params.subscribe(
params => {
this.gtin = params['gtin'];
this.getProduct(this.gtin);
}
);
const genericnameControl = this.productForm.controls["genericname_en"];
genericnameControl.valueChanges.subscribe(value => {
//this.productForm.controls['genericname_en'].markAsDirty();
if (value === '-1') {
genericnameControl.setErrors({
required: true
});
}
});
const dosageformControl = this.productForm.controls["dosageform"];
dosageformControl.valueChanges.subscribe(value => {
//this.productForm.controls['dosageform'].markAsDirty();
if (value === '-1') {
dosageformControl.setErrors({
required: true
});
}
});
const shelflifeControl = this.productForm.controls["shelflife"];
shelflifeControl.valueChanges.subscribe(value => {
//this.productForm.controls['shelflife'].markAsDirty();
if (value === '-1') {
shelflifeControl.setErrors({
required: true
});
}
});
}
saveProduct(): void {
if (this.productForm.dirty && this.productForm.valid) {
console.log("INSIDE SAVE PRODUCT!!!!!! " + this.productForm.value.productcategory);
let gtin = this.route.snapshot.params['gtin'];
let p = Object.assign({}, this.product, this.productForm.value);
this.productDataService.saveProduct(p,gtin)
.subscribe(
() => this.onProductComplete(),
(error: any) => this.errorMessage = <any>error
);
} else if (!this.productForm.dirty) {
this.onProductComplete();
}
}
【问题讨论】:
-
请给我们一些代码! HTML 和组件
-
我们无法确定您使用的是模板驱动表单还是模型驱动表单。
-
我使用的是模型驱动表单,并且我已经添加了 HTML 代码
-
这还不够。你的
<form>元素在哪里?你是如何实例化表单的? -
检查一下,我没有发现任何问题:plnkr.co/edit/…
标签: angular angular2-forms angular2-directives