我尝试使用反应式表单,我只需要为每个填充的项目创建键但是因为创建的第一个时刻,如果字段未满,它会发送 null 而我想要一个不为空的字段我发送的所有代码是:
form: FormGroup;
constructor() {
this.form = new FormGroup({
basePrice: new FormControl(null, [customeValidator.numberOnly()]),
discountValue: new FormControl(null, [customeValidator.numberOnly()]),
roomViewCount: new FormControl(null, [customeValidator.numberOnly()])
});
}
onCalcute() {
console.log(this.form.value);
if (this.form.valid) {
this.roomData = {
basePrice: this.form.value.basePrice,
discountValue: this.form.value.discountValue,
roomViewCount: this.form.value.roomViewCount
};
this.clicked = true;
}
}
Run Code Online (Sandbox Code Playgroud)
如果其中只有一个已满 console.log 返回
{
basePrice: 20,
discountValue: null,
roomViewCount: null
}
Run Code Online (Sandbox Code Playgroud)
但是我需要:
{
basePrice: 20
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用 ngx-material-timepicker ,如下面的代码,但我无法更改任何 CSS 类,我正在关注此链接https://www.npmjs.com/package/ngx-material-timepicker
这是我的代码
<div class="clock">
<input placeholder="24hr format" aria-label="24hr format" [ngxTimepicker]="fullTime" [format]="24" readonly />
<ngx-material-timepicker
[appendToInput]="true"
[disableAnimation]="true"
[theme]="oktTheme"
(timeSet)="onTimeset($event)"
#fullTime
></ngx-material-timepicker>
</div>
Run Code Online (Sandbox Code Playgroud)
这是ts代码
oktTheme = {
container: {
bodyBackgroundColor: "#424242",
buttonColor: "#fff"
},
dial: {
dialBackgroundColor: "#555"
},
clockFace: {
clockFaceBackgroundColor: "#555",
clockHandColor: "#01806b",
clockFaceTimeInactiveColor: "#fff"
}
};
Run Code Online (Sandbox Code Playgroud) 我使用以下代码:
::ng-deep{
.mat-select-panel{
border-radius: 10px;
border: 1px solid gray;
position:absolute;
top: 50px;
}
}
Run Code Online (Sandbox Code Playgroud)
我期望它看起来像这样:
但仍然是这样的:
从选择的地方打开它很难看
我想显示一些基于响应的 html 代码,但在接收响应之前渲染 HTML。
网页:
<div *ngIf="medias.length > 0">
some text
</div>
<div *ngIf="!medias.length > 0">
some other text
</div>
Run Code Online (Sandbox Code Playgroud)
时间:
ngOnInit(): void {
this.getMedias();
}
medias: Array<T> = [];
getMedias() {
this.controllerApiService
.getById()
.subscribe(data => {
this.medias = data
});
}
Run Code Online (Sandbox Code Playgroud)
根据上面的代码,首先显示“一些其他文本”,收到响应后,显示“一些文本”
我尝试使用skip()和skipWhile()来处理,但没有任何改变。
我有一个表单组,我需要显示 ngIf 所需的输入之一。但即使此输入不存在,formcontrol也会检查它并返回表单无效。我的代码就像这样的 html:
<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
<label>mobile</label>
<input type="text" formControlName="mobile" />
<div *ngIf="!hasInfo">
<label>firstName</label>
<input type="text" formControlName="firstName" />
<label>lastName</label>
<input type="text" formControlName="lastName" />
</div>
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
TS:
constructor(){
this.formBuilder();
}
ngOnInit() {
if (accountResponse.length > 0) this.hasInfo= true;
}
formBuilder() {
this.myForm= new FormGroup({
mobile: new FormControl(null, Validator.required()),
firstName: new FormControl(null, Validator.required()),
lastName: new FormControl(null, Validator.required()),
});
}
onSubmit(){
if(this.myForm.valid){
console.log("valid")
}else{
console.log("not valid")
}
}
Run Code Online (Sandbox Code Playgroud)
如果 hasInfo 为 true 则不显示名字和姓氏,我的表单应返回有效但始终无效