简体   繁体   English

使用 Angular Forms 而不是 ngModel

[英]Using Angular Forms instead of ngModel

I have following Code with ngModel, which is working.我有以下代码和 ngModel,它正在工作。 HTML: HTML:

<div class="container">
  <div class="search-name">
      <input class="form-control" type="text" name="search" [(ngModel)]="searchText" autocomplete="on" placeholder=" SEARCH  ">
  </div>
  <ul *ngFor="let name of names | filter:searchText">
      <li>
          <span>{{name.country}}</span>
      </li>
  </ul>
</div>

TypeScript: TypeScript:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'search-filter-angular';
  searchText: any;
  names = [
    { country: 'Adil'},
    { country: 'John'},
    { country: 'Jinku'},
    { country: 'Steve'},
    { country: 'Sam'},
    { country: 'Zeed'},
    { country: 'Abraham'},
    { country: 'Heldon'}
];
}

How can I write this code with angular forms?我如何用 angular forms 编写这段代码? I read there is also a two way data binding.我读到还有一种双向数据绑定。

Can please someone help?可以请人帮忙吗?

You can just use the following code:您可以只使用以下代码:

HTML: HTML:

<div class="container">
  <div class="search-name">
      <input class="form-control" type="text" name="search" [formControl]="searchText" autocomplete="on" placeholder=" SEARCH  ">
  </div>
  <ul *ngFor="let name of names | filter: (searchText.valueChanges | async)">
      <li>
          <span>{{name.country}}</span>
      </li>
  </ul>
</div>

TS: TS:

title = 'search-filter-angular';
  searchText = new FormControl();
  names = [
    { country: 'Adil' },
    { country: 'John' },
    { country: 'Jinku' },
    { country: 'Steve' },
    { country: 'Sam' },
    { country: 'Zeed' },
    { country: 'Abraham' },
    { country: 'Heldon' },
  ];

Please remember to import ReactiveFormsModule in your module.请记住在您的模块中导入ReactiveFormsModule

Remove the pipe, use valuechanges and filter with rxjs.删除 pipe,使用 valuechanges 并使用 rxjs 进行过滤。

Template模板

<div class="container">
  <div class="search-name">
    <input
      class="form-control"
      type="text"
      name="search"
      [formControl]="searchText"
      autocomplete="on"
      placeholder=" SEARCH  "
    />
  </div>
  <ul *ngFor="let name of filteredNames$ | async">
    <li>
      <span>{{ name.country }}</span>
    </li>
  </ul>
</div>

TS TS

searchText = new FormControl();

  names: MyName[] = [
    { country: 'Adil'},
    { country: 'John'},
    { country: 'Jinku'},
    { country: 'Steve'},
    { country: 'Sam'},
    { country: 'Zeed'},
    { country: 'Abraham'},
    { country: 'Heldon'}
  ];

  filteredNames$: Observable<MyName[]> = this.searchText.valueChanges.pipe(
    map(filter => filter ? this.names.filter(name => name.country.includes(filter)) : this.names),
    startWith(this.names),
  );

Play with this on stackblitz: https://stackblitz.com/edit/angular-wigwzh?file=src/app/app.component.ts在 stackblitz 上玩这个: https://stackblitz.com/edit/angular-wigwzh?file=src/app/app.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM