Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular中的Issue witch async(列出Web Api中的元素)_Angular_Typescript_Asynchronous - Fatal编程技术网

angular中的Issue witch async(列出Web Api中的元素)

angular中的Issue witch async(列出Web Api中的元素),angular,typescript,asynchronous,Angular,Typescript,Asynchronous,以下是代码片段: this.languages = await this.languageService.getAllLanguages().toPromise() this.languages.forEach(async (value, index) => { console.log(value.name) this.originLanguageOption.setDropdownOption ( value.name,

以下是代码片段:

this.languages = await this.languageService.getAllLanguages().toPromise()

    this.languages.forEach(async (value, index) => {
      console.log(value.name)
      this.originLanguageOption.setDropdownOption (
        value.name,
        index
      )

      this.originLanguageOptions.push(
        this.originLanguageOption     
      )
    })

    this.originLanguageDropdown.setDropdown(
      'Origin language',
      'Choose Language',
      this.originLanguageOptions
    ) 
问题: 控制台日志工作正常,它只是列出DB中的所有元素,但当我尝试将数据分配给“this.originLanguageOptions”时,它会覆盖列表中以前的所有元素。该数组看起来像: 第一圈:

  • 英式
第二圈:

  • 德文
  • 德文
第三圈:

  • 法国
  • 法国
  • 法国
等等

应该是这样的: 第三环路

  • 英式
  • 德文
  • 法国

在foreach的每次迭代中,您都在同一实例上使用setDropdownOption()。我知道你应该在每次迭代中创建一个选项,然后将其添加到数组中。

你能为这个组件添加代码吗?不需要,下面的朋友回答了如何修复它。谢谢Somg,我的脸在移动创建选项后循环:O.O,我的大脑在意识到我有时有多笨后:xD。不管怎样,谢谢你:D