如何在[(ngModel)]中使用Map()?

时间:2018-12-12 12:26:35

标签: angular typescript

我正在尝试通过特定键将特定值输入到映射中。

在我的代码中,我使用ngFor遍历数组。我在每次迭代中得到的项目应该是地图的键值,而用户输入了相应的值。

重要信息:变量userInputValue不存在。这就是我的问题的重点:如何获取userInputValue,以便可以将其应用于地图? 希望您能理解我的意思。

item是任何字符串,例如"Name",而userInputValue就是用户在input field中键入的内容,在这种情况下如何获取后者?

在我的Component.html

<form *ngFor="let item of myStringArray">
    <input [(ngModel)]="map.set(item, userInputValue)">
</form>

在我的Component.ts

public map = new Map<string, string>();

public myMethod() {
    this.stringInputs.forEach((item) => {
       this.eintrag.raumgroesseQM = this.map.get(item);
    });
}

是否可以通过这种方式使用Map?如果可以,我如何正确地使用它;如果不能,是否还有其他方法可以达到这个目标(当然,除了自己上课)?

我当前的方法使用定界符分割,如下所示:

// the string would be something like: "Name;34"

this.finalInputWithContext.forEach((item) => {

  if (item.split(";")[0] === "Name") {
    concole.log(item.split(";")[1]); // will print 34
  }

});

当然,此解决方案也可以使用,但是有人告诉我改用Map。

1 个答案:

答案 0 :(得分:7)

您可以将<?php $client = new http\Client; $request = new http\Client\Request; $body = new http\Message\Body; $body->addForm(NULL, array( array( 'name' => 'photo', 'type' => null, 'file' => 'user_path/2018-11-09 o 15.00.48.png', 'data' => null ) )); $request->setRequestUrl('url'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setHeaders(array( 'Postman-Token' => 'f6154fff-46f4-47d0-a7c3-98d7de8d0f24', 'Cache-Control' => 'no-cache', 'Content-Type' => 'application/x-www-form-urlencoded' )); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody(); 分为两个部分[(ngModel)][ngModel],然后在每个部分中调用适当的(ngModelChange)方法。用户键入的值由Map事件的$event参数给定。

ngModelChange

有关演示,请参见this stackblitz

相关问题