Angular2从ngFor绑定ngModel

核心

所以我正在textarea动态渲染,ngFor但是我不确定如何将其ngModel绑定到函数中。

<div *ngFor="let inputSearch of searchBoxCount; let i = index" [ngClass]="{'col-sm-3': swaggerParamLength=='3', 'col-sm-9': swaggerParamLength=='1'}">
   <textarea name="{{inputSearch.name}}" id="{{inputSearch.name}}" rows="3" class="search-area-txt" attr.placeholder="Search Product {{inputSearch.name}}"
     [(ngModel)]="inputSearch.name"></textarea>
  </div>

textarea示例: 在此处输入图片说明

textarea的是基于我从API调用得到我的情况下,响应的长度渲染searchBoxCount基本上searchBoxCount.length,如果它length是= 1那么它只会呈现1textarea如果3,那么它会显示3 textareasobj的名称不同(例如:id / email / whatever),因此ngModel基于json对象中的obj名称。

如何绑定inputSearch.name我的功能getQueryString()

 getQueryString() {
    this.isLoading = true;
    let idInputValue = inputSearch.name; //bind it here

    return "?id=" + idInputValue
        .split("\n") // Search values are separated by newline and put it in array collection.
        .filter(function(str) {
            return str !== ""
        })
        .join("&id=");
}

调用getQueryString()的搜索功能

searchProduct() {
    let queryString1 = this.getQueryString();
    this._searchService.getProduct(queryString1)
        .subscribe(data => {
            console.log(data);
        });
}

我知道如何做到这一点,如果ngModel没有来自未来的ngFor,有另一种方式来获得从该值textareangModel也许那是唯一的方法,或者如果我仍然可以使用ngModel

史蒂文·卢克

当前状态摘要

首先,让我总结一下您的数据在哪里。您有一个或多个名为的对象的列表searchBoxCount列表中的每个元素都是一个具有name属性的对象,因此,例如,您可以调用let name = this.searchBoxCount[0].name;以获取列表中第一个对象的名称。

在HTML模板中,您可以使用ngFor遍历searchBoxCount列表中的所有对象,并在每次迭代中将对象分配给名为的本地变量(ngFor)inputSearch然后,将每个循环迭代中创建的文本区域的输入绑定到该name迭代inputSearch对象属性。

如何获取数据

此处的关键是inputSearch与存储在searchBoxCount某个特定索引(第一个对象的索引为0等)的对象相同。因此,当ngModel绑定到时,inputSearch.name它也绑定searchBoxCount[n].name在ngFor外部,您将遍历searchBoxCount列表以获取所需的每个名称。

作为结果

根据原始帖子的评论,听起来您可以拥有一个或多个需要包含在查询字符串输出中的名称。这意味着getQueryString()要工作,您必须遍历列表(或在这种情况下,让列表为我们循环):

getQueryString() {
    this.isLoading = true;
    let result : string = "?id=";
    this.searchBoxCount.forEach(
        (inputSearch:any) => {  //Not the same variable, but same objects as in the ngFor
            result = result + inputSearch.name + "&id=";
    });
    result = result.slice(0, result.length - 4);  //trim off the last &id=
    return result;
}

编辑:具有不同名称的多个不同字段

从这篇文章的评论中可以很清楚地看到,每个inputSearch都有自己的键供查询字符串使用,该键存储在name属性中。您需要保留该名称,这意味着您无法将ngModel绑定到该名称。否则,用户将通过输入自己的文本来破坏名称,并且将无法找回正确的密钥。为此,您需要将ngModel绑定存储到inputSearch对象的其他属性。我将假定该对象具有value属性,因此它看起来像这样:

{
    name: "id",
    value: "33\n44"
}

也就是说,每个inputSearch都有一个名称,并且该值将具有一个或多个值,并用新行分隔。然后,您必须将HTML模板更改为此:

<div *ngFor="let inputSearch of searchBoxCount; let i = index" 
     [ngClass]="{'col-sm-3': swaggerParamLength=='3', 'col-sm-9': 
     swaggerParamLength=='1'}">
   <textarea name="{{inputSearch.name}}" 
             id="{{inputSearch.name}}" rows="3" class="search-area-txt" 
             attr.placeholder="Search Product {{inputSearch.name}}"
             [(ngModel)]="inputSearch.value"></textarea>
</div>

请注意,我将ngModel从更改inputSearch.name (如果没有开头的值,?允许为null) 然后,getQueryString()方法如下所示: inputSearch?.value inputSearch.value

getQueryString() {
    let result:string = "?";

    //for each of the input search terms...
    this.searchBoxCount.forEach( (inputSearch:any) => {
        // first reparse the input values to individual key value pairs
        let inputValues:string = inputSearch.value.split("\n")
             .filter(function(str) { return str !== "" })
             .join("&" + inputSearch.name + "=");
        // then add it to the overall query string for all searches
        result = result + 
             inputSearch.name + 
             "=" + 
             inputValues +
             "&"
    });
    // remove trailing '&'
    result = result.slice(0, result.length - 1);
    return result;
}

注意,使用RxJs可能更容易,但是我正在测试香草javascript。

使用此方法,如果用户输入了两个ID(33和44),一个sku和两封电子邮件,则结果将是 ?id=33&id=24&sku=abc123&[email protected]&[email protected]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章