角材料自动完成不保留选择

时间:2019-11-22 23:54:15

标签: angular angular-material

我正在尝试使用Angular Material来实现自动完成输入字段,该字段从后端服务器中获取用户列表。

我设法将数据放入自动完成下拉列表并进行了过滤。不幸的是,当从下拉列表中选择一个项目时,它不会显示在输入框中。

控制器:

export class SignalMutateComponent implements OnInit {

  visible = true;
  selectable = true;
  removable = true;
  addOnBlur = true;
  readonly separatorKeysCodes: number[] = [ENTER, COMMA];
  options: User[] = []
  filteredOptions: Observable<User[]>;
  assigneeId = new FormControl();


  private _signalMutate: Signal;
  @Input() set signalMutate(signalMutate: Signal) {
    this._signalMutate = signalMutate
    this.signalForm.patchValue(signalMutate);
    if(signalMutate.labels) {
        this.labels = signalMutate.labels.split(',');
        if(this.labels[0] == "") {this.labels.shift();}
      } else {
        this.labels = [];
      }

  }


get signalMutate() {
  return this._signalMutate
}

  signalForm: FormGroup;
  loading = false;
  submitted = false;
  error: string;
  formSignalValue: Signal;
  labels: [string] = [];

  constructor(
    private formBuilder: FormBuilder,
    private router: Router,
    private authenticationService: AuthenticationService,
    private userService: UserService,
    private signalService: SignalService,
    private snackBar: MatSnackBar

  ) { }

  ngOnInit() {

    this.userService.getFiltered("active")
      .subscribe(users => {this.options = users as User[]; });

    this.signalForm = this.formBuilder.group({
      title: [''],
      description: ['', Validators.required],
      problem: ['', Validators.required],
      value: ['', Validators.required],
      requestor: ['', Validators.required],
      labels: [''],
      id:[''],
      assigneeId: ['']
  });

    this.filteredOptions = this.assigneeId.valueChanges
    .pipe(
      startWith(''),
      map(value => typeof value === 'string' ? value : this.updateAssignee(value)),
      map(fullName => fullName ? this._filter(fullName) : this.options.slice())
    );

  }

  displayFn(user?: User): string | undefined {
    return user ? user.fullname : undefined;
  }

  private _filter(name: any): User[] {
    console.log("name");
    console.log(name);
    const filterValue = name.toLowerCase();
    console.log(filterValue);
    return this.options.filter(option => option.fullName.toLowerCase().indexOf(filterValue) === 0);
  }


  updateAssignee(id: number) {

      console.log('yes: '+id);
      this.signalForm.patchValue({assigneeId: id});

  }
....

HTML文件:

 <div class="uk-width-1-1">
                            <mat-form-field class="formFieldWidth700">
                                    <input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="assigneeId" [matAutocomplete]="auto" [ngClass]>
                                    <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
                                      <mat-option *ngFor="let option of filteredOptions | async" [value]="option.id">
                                        {{option.fullName}}
                                      </mat-option>
                                    </mat-autocomplete>
                                  </mat-form-field>
                    </div>

任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

找到了答案:

Variable  PointsTo  Address With Data ...
str2        ->      0x1000:      | J | o | h | n | \0 |
str1        ->      0x1100:      | M | a | r | y | \0 |

显示功能需要使用ID,因为这是在值中选择的内容。