-1

在此先感谢您的回复,我正在检查 ddbb 以带上一些项目的名称并将它们绘制在我的 ts 中,如下所示:

应用批处理应用管理.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.";
      });
    }
  }

和我的 app-batch-app-management.html

 <div class="h2 mt-16">Application Management of {{deviceNameList}}</div>

我的ts中的上述条件,并不总是绘制所有项目,我认为是因为条件,所以我想删除它,但它首先画了一个逗号,我怎么能删除第一个逗号?

4

3 回答 3

0

使用基本正则表达式,您可以用字符串中的空格替换逗号:

",your,String".replace(/^,/, '')

注意:正则表达式中的 ^ 将获取第一个要删除的字符

于 2020-06-22T12:20:55.320 回答
0

不要使用Promise.then内部循环,这会导致不良影响(这就是您的条件检查失败的原因,因为它引用了一个闭包变量)。相反,await它,所以你的检查将起作用,或者解决逗号问题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(', ');
于 2020-06-22T12:27:39.280 回答
0

你可以只使用substring方法:

this.deviceNameList = this.deviceNameList.substring(2);
于 2020-06-22T12:15:31.667 回答