I have this pipe, secondsToHms
, to convert from seconds to hours, minutes and seconds:
transform(seconds: number): string {
const minutes = Math.floor(seconds / 60); //minutes
const secs = seconds % 60; //seconds
const hours = Math.floor(minutes / 60); //hours
const mins = minutes % 60; //minutes
return (
'' +
(hours < 10 ? hours.toString().padStart(2, '0') : hours.toString()) +
mins.toString().padStart(2, '0') +
secs.toString().padStart(2, '0')
);
}
In my HTML, I'm trying to display the value like this, but the time doesn't display as I want (HH:mm:ss) and I don't know why. I'm using ngx-mask inside the input.
<ng-template [ngIf]="control.value.type === 'TYPE1'">
<div class="input-group mb-2">
<input
(ngModelChange)="setDirty(true)"
formControlName="quantity"
type="text"
class="form-control text-center"
placeholder="00:00:00"
mask="00:m0:s0"
[value]="control.value.quantity | secondsToHms"
/>
</div>
</ng-template>
The control
variable is accessing in the template via this loop:
<ng-container *ngFor="let control of dataArr.controls;
let i = index">
And this is the dataArr
function:
get dataArr() {
return this.myForm.get('allData') as FormArray;
}
I need to do the conversion in the template since the pipe only applies when type is equal to TYPE1 which only happens here [ngIf]="control.value.type === 'TYPE1'"
.
I'm using this function to create form controls, don't know if it's relevant to this case.
addDataFormGroup(data: DataModel) {
this.dataArr.push(
this.fb.group({
data: data.data,
dataType: data.dataType,
quantity: data.quantity,
})
);
}