动态添加新的md自动完成元素

时间:2017-07-29 11:27:57

标签: angularjs angular-material

<div layout="row" ng-repeat="choice in choices">
        <md-autocomplete
          ng-disabled="autoCompleteOptions.isDisabled"
          md-no-cache="autoCompleteOptions.noCache"
          md-selected-item="autoCompleteOptions.selectedItem"
          md-search-text="autoCompleteOptions.searchText"
          md-items="item in querySearch(fabrics, autoCompleteOptions.searchText, 'fab_id')"
          md-min-length="0"
          md-item-text="item.fab_id"
          placeholder="Search for a Fabric here">
        <md-item-template>
          <span md-highlight-text="autoCompleteOptions.searchText" md-highlight-flags="^i">{{item.fab_id}}</span>
        </md-item-template>
          <md-not-found>
            No FabIds matching "{{autoCompleteOptions.searchText}}" were found.
          </md-not-found>
      </md-autocomplete>
</div>

如何在此处创建动态搜索文本和所选项目变量?我希望这些自动完成元素是不同的,但无法找到在字符串中传递动态名称的方法。

1 个答案:

答案 0 :(得分:1)

您可以选择对象数组,对于ng-repeat循环的每次迭代,choice都是对choices数组中对象的引用。您可以在该数组中包含searchTextselectedItemfieldName等属性。我已经创建了自己的示例来解决您的问题,因此您的等效模板可以是:

<div  ng-repeat="choice in ctrl.choices">
   <md-autocomplete

      md-selected-item = "choice.selectedItem"
      md-search-text-change = "ctrl.searchTextChange(choice.searchText)"
      md-search-text = "choice.searchText"
      md-selected-item-change = "ctrl.selectedItemChange(item)"
      md-items = "item in ctrl.querySearch(choice.searchText)"
      md-item-text = "item.display"
      md-min-length = "0"
      placeholder = "US State? {{choice.fieldName}}">

      <md-item-template>
         <span md-highlight-text = "choice.searchText"
            md-highlight-flags = "^i">{{item.display}}</span>
      </md-item-template>

      <md-not-found>
         No states matching "{{choice.searchText}}" were found.
      </md-not-found>
   </md-autocomplete>
</div>

在控制器中以及项目teplate数据所需的其他功能中,请执行以下操作:

this.choices = [{searchText: '', selectedItem: '', fieldName: 'Field 1'},
            {searchText: '', selectedItem: '', fieldName: 'Field 2'},
            {searchText: '', selectedItem: '', fieldName: 'Field 3'}];

以下是plunker示例的链接:https://plnkr.co/edit/hcBtQ45GUY15XUOfjEUr?p=preview