- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Angular Reactive Forms 应用程序中使用 PrimeNG 日历控件。在我的应用程序中,我在 Primefaces 对话框中有一个带有日历控件的表单。
当在日历控件中键入无效的日期值时,我会在对话框关闭时以编程方式将控件的值更新为预定义的回滚日期值,以便再次显示时表单处于有效状态。
但是,我发现以编程方式更新日历控件的值并不能始终使用新值更新 UI。
我正在使用 FormGroup 并尝试了 setValue 和 patchValue。我还尝试在日历控件上显式使用 setValue 和 patchValue 以及 formGroup 的重置方法。问题依旧。
只是想知道是否有人可以就我在代码中出错的地方提出建议?
我在 http://plnkr.co/edit/xc4ygZ?p=info 创建了一个 plunker , 举例说明。 Angular 组件和模板的代码包含在下面....
import {
Component
} from '@angular/core';
import {
FormBuilder
} from '@angular/forms';
import {
FormGroup
} from '@angular/forms';
import {
OnInit
} from '@angular/core';
import {
Validators
} from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: 'app/app.template.html'
})
export class AppComponent {
birthDate: Date;
form: FormGroup;
formErrors = {
'setdate': '',
};
validationMessages = {
'setdate': {
'required': 'Set date is required.',
'invalidDate': 'Invalid Date.'
},
};
constructor(private fb: FormBuilder) {
this.birthDate = new Date();
}
ngOnInit(): void {
this.buildForm();
}
onSubmit(): void {
}
setSetDate(): void {
let today = new Date();
this.form.patchValue({
setdate: today
});
}
private buildForm(): void {
this.form = this.fb.group({
setdate: [this.birthDate, [Validators.required]]
});
this.form.valueChanges
.subscribe(data => this.onValueChanged(data));
//this.onValueChanged();
}
private onValueChanged(data ? : any): void {
let _self = this;
if (!this.form) {
return;
}
const form = this.form;
console.log("onValueChanged()");
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + '\n';
}
}
}
}
}
<p> Enter a date that is invalid, e.g. edf. Then try clicking the button to programmatically reset the date</p>
<p> The UI for the calendar control does not consistently update with the new date. </p>
<form autocomplete="off" [formGroup]="form" novalidate (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="setdate" class="control-label col-xs-4">Set Date</label>
<div>
<p-calendar #theDate id="setdate" formControlName="setdate" showTime="true" hourFormat="24" dateFormat="dd-mm-yy" [showIcon]="true" placeholder="Set Date" required>
</p-calendar>
<p>SetDate Value :: {{ theDate.value }}</p>
<p>SetDate Valid :: {{ this.form.controls['setdate'].valid }}</p>
<p>SetDate Errors :: {{ this.form.controls['setdate'].errors | json }}</p>
</div>
<div *ngIf="formErrors.setdate" class="alert alert-danger">
{{ formErrors.setdate }}
</div>
</div>
<button type="button" class="btn" (click)="setSetDate()">Test SetDate</button>
</form>
最佳答案
我有两件事导致 Reactive Forms 和 PrimeNG 不能一起工作。
我使用的是 this.myForm.reset()
,它清除了 PrimeNG 组件上的必要数据。
解决方案
我使用 this.myForm.patchValue({myFormControlName: 'my reset value'})
重置值。
如果您愿意,当然可以使用 this.myForm.setValue()
。
在我的日历上我做了什么Yaroslav Admin描述 here .本质上我没有传递日期对象。我将 unix 时间戳作为数字类型传递。
解决方案
代替:
const time = 1562162176;
this.form.controls.calendarField.setValue(time)
这样做:
const time = new Date(1562162176);
this.form.controls.calendarField.setValue(time)
关于angular - 当日历控件处于无效状态时,PrimeNG 日历控件 Reactive Forms 更新值看不到更新日历 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167856/
我正在尝试使用下拉菜单过滤 primeNG 表中的列,但我只想在按下“应用”按钮时应用过滤器。现在过滤发生在 onChange 事件中。如果我删除 onChange 事件,过滤器将不起作用 这是没有
需要在 primeNg 的文本框内放置一个清晰的图标。单击它应该会清除该字段。 最佳答案 对于 Primefaces 关于prim
我想在我自己的组件中包装一个 primeng 自动完成组件,但不知道如何提供 formControlName: 错误:未捕获( promise ):错误:formControlName 必须与父 fo
当我将滚动条应用到主菜单时,我遇到了子菜单被切断的问题,但我无法让子菜单显示在滚动条上方。 下面是一个简单的代码:https://stackblitz.com/edit/angular-cyvv7d
我使用 PrimeNG 创建了一个数据表。我刚刚为数据表创建了 2 个标题。我实际上会从服务器获取数据,因此如果没有数据,我不希望显示默认的“未找到记录消息”。 如果表格中没有数据,我希望表格为空。
我从 Angular 2 RC5 和 PrimeNG 1.0.0-beta.13 beta 开始。我将 PrimeNg 组件作为 xModules 加载,并且在显示数据表、输入、SelectItem、
我正在使用一个动态对话框,我想在页脚上有一些按钮,但是在这个组件中似乎只允许文本作为页脚。 const ref = this.dialogService.open(Te
我正在尝试在按钮上添加条件工具提示。使用primeng工具提示组件: 它不工作。 但是,没有“tooltipDisabled”的工具提示工作正常。 有人可以帮帮我吗? 最佳答案 使用
我想用styleClass Togglebutton 的属性(property)成分。如 another post 中所述,我认为使用以下方法很简单: styleClass="test" 在我的 cs
根据https://www.primefaces.org/primeng/#/table的文档,重置方法应为“重置排序,过滤器和分页器状态”。 问题在于,重置表方法未从UI中删除过滤器。 (尽管重置后
我已经使用 PrimeNG Table 创建了一个 Angular 应用程序。我在这里查看了 PrimeNG 文档 ( https://www.primefaces.org/primeng/#/tab
我正在使用 PrimeNg 库。它有很好的组件,默认创建可拖动的对话框。但在某些情况下,我正在使用 PrimeNg 的 API dialogService , 动态创建对话框。注意 this.dial
使用 PrimeNg 的动态对话示例展示了目标对话如何获取数据并显示。 我可以通过创建一个带有 observable 的服务来将数据传递给对话,但我知道对话服务有一些可用的参数(如数据)可以传递。 对
我在 Angular 5 中使用 PrimeNG 6.0.2,但我遇到了 的问题表插入。我切换到 Table 因为 DataTable 已被弃用。现在,我无法像以前那样访问过滤值。 假设我通过 Vie
我对 primeNG 表有疑问。表格有很多列,在列切换上,列宽在变化,表格看起来很乱。 表格可滚动和可调整大小,因此根据文档,由于技术原因不支持自动布局。 所以也许有一些解决方法,或者是否有一种方法可
在我的 Angular 项目中,我使用 primeNG 库我正在尝试使数据表中的行可编辑,所以我按照文档进行操作[ https://www.primefaces.org/primeng/#/table
抱歉菜鸟问题... 使用primeng包时,推荐的将焦点设置在控件上的方法是什么?使用传统输入控件时,我设置了一个表单变量 (#variable) 并使用 @ViewChild 获取对它的引用,以便我
我需要在 PrimeNG 数据表中显示大量数据(400.000 条记录)。为此,我需要一个延迟加载表,因为您无法一次将所有数据加载到表中(这会使浏览器崩溃)。 为了创建表,我使用了以下技术: Angu
我使用 PrimeNG 5.0.1、Angular 5.1 中的组件日历,它允许选择日期和时间: 但是 timepicker 在重新加载页面时被卡住。每次显示选择器时是否可以更新时间值? 我还没有发
在 Angular v7 项目中使用 PrimgNG v7.1.3。 在一个简单的组件中实现了多选 ( https://www.primefaces.org/primeng-7.1.3/#/multi
我是一名优秀的程序员,十分优秀!