Thanks in advance for your response, I'm checking a ddbb to bring the names of some items and paint them in my ts like this:
app-batch-app-management.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-batch-app-management',
templateUrl: './batch-app-management.component.html',
styleUrls: ['./batch-app-management.component.scss']
})
constructor(private location: Location,
private activatedRoute: ActivatedRoute,
private deviceService: DeviceService,
private appManagementService: AppManagementService) {
this.deviceNameList = "";
this.mgmtErrorMessage = null;
this.deviceId = "";
this.mgmtErrorMessage = null;
}
ngOnInit() {
this.activatedRoute.queryParams.subscribe(params => {
this.mgmtErrorMessage = null;
this.deviceIdList = params["deviceList"];
this.deviceId = this.deviceIdList[0];
});
for (let i = 0; i < this.deviceIdList.length; i++) {
this.deviceService.getDeviceName(this.deviceIdList[i]).then(deviceName => {
// if (i === 0) {
// this.deviceNameList = deviceName;
// } else {
this.deviceNameList += ", " + deviceName;
// }
}).catch(err => {
this.mgmtErrorMessage = "Oops, could not get the device configuration.";
});
}
}
and my app-batch-app-management.html
<div class="h2 mt-16">Application Management of {{deviceNameList}}</div>
The condition above in my ts, doesn't paint all the items always, I think because of the condition, so I want to remove it, but it's painting a comma in the first place, how could I remove the first comma?
You can just use substring
method:
this.deviceNameList = this.deviceNameList.substring(2);
Using Basic regex, you can replace the comma with a blank in the string:
",your,String".replace(/^,/, '')
Note: ^ in regex will get the first Character to remove
Do not use Promise.then
inside loops, which will cause undesirable effects (that's why your condition check fails, because it refers to a closure variable). Instead, await
it, so your check will work, or alternatively solve the comma problem with join()
:
const names = [];
for (let i = 0; i < this.deviceIdList.length; i++) {
try {
const deviceName = await this.deviceService.getDeviceName(this.deviceIdList[i]);
names.push(deviceName);
} catch (err) {
this.mgmtErrorMessage = "Oops, could not get the device configuration.";
}
}
this.deviceNameList = names.join(', ');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.