代码之家  ›  专栏  ›  技术社区  ›  Maniraj Murugan

使用带<input>标记的角度的多选下拉列表

  •  0
  • Maniraj Murugan  · 技术社区  · 6 年前

    我正在构建Angular6应用程序,其中需要使用 <input> 不使用文本框 <select>

    Html格式 :

    <form>
      Select User: <input type="text" name="user" [value]="users"><br>
      <button type="submit"> Submit </button>
    </form>
    

    :

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
        users = ['First User', 'Second User', 'Third User', 'Fourth User'];
    }
    

    我也需要用 纯Javascript,Tpescript ,以及 没有第三方或jquery .

    控件

    我找了很多,但找不到合适的解决办法。 请帮助我取得结果。

    斯塔克布利茨 https://stackblitz.com/edit/angular-v7kmbq

    2 回复  |  直到 6 年前
        1
  •  2
  •   Bhimashankar Mantur    6 年前

    这是工作代码,我使用了[(ngModel)]formcontrols:

    https://stackblitz.com/edit/angular-qjrhs5?file=src%2Fapp%2Fapp.component.css

        2
  •  2
  •   Juan    6 年前

    检查StackBlitz: Dropdown Example

    <button type="button" (click)="clear()">Clear</button>
    
    <div class="autocomplete">
        <input name="suggestion" type="text" placeholder="User" (click)="suggest()" [formControl]="typeahead">
    
        <div class="autocomplete-items" *ngIf="show">
          <div *ngFor="let s of suggestions" (click)="selectSuggestion(s)">{{ s }}</div>
        </div>
    </div>
    

    TS文件:

    import { Component } from '@angular/core';
    import { FormControl } from '@angular/forms';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular';
    
      suggestions: string [] = [];
      suggestion: string;
      show: boolean = false;
      typeahead: FormControl = new FormControl();
    
      fieldHistory: string [] = [];
    
      suggest() {
        this.suggestions = this.users;
        this.show = true;
      }
    
      selectSuggestion(s) {
          this.suggestion = "";
    
          var index = this.fieldHistory.findIndex(function(element) {
              return element === s;
          });
    
          if (index === -1) {
              this.fieldHistory.push(s);
          } else {
              var firstPart = this.fieldHistory.slice(0, index);
              var secondPart = this.fieldHistory.slice(++index);
    
              this.fieldHistory = firstPart.concat(secondPart);
          }
    
          for (let i = 0; i < this.fieldHistory.length; i++) 
               this.suggestion = this.suggestion + " " + this.fieldHistory[i];
    
          this.typeahead.patchValue(this.suggestion);
          this.show = false;
      }
    
      clear() {
          this.suggestion = "";
          this.fieldHistory.pop();
    
          for (let item of this.fieldHistory) 
              this.suggestion = this.suggestion + " " + item;
    
          this.typeahead.patchValue(this.suggestion);
      }
    
      users = ['First User', 'Second User', 'Third User', 'Fourth User'];
    }
    

    CSS文件:

    .autocomplete {
        position: relative;
        width: 100%;
    }
    
    .autocomplete-items {
        position: absolute;
        border: 1px solid #d4d4d4;
        border-radius: 4px;
        margin-top: 15px;
        top: 100%;
        left: 0;
        right: 0;
    }
    
    .autocomplete-items div {
        padding: 10px;
        cursor: pointer;
        background-color: #fff;
        border-bottom: 1px solid #d4d4d4;
    }
    
    .autocomplete-items div:hover {
        background-color: #e9e9e9; 
    }
    

    import { ReactiveFormsModule } from '@angular/forms';