简体   繁体   English

可重复使用的 angular 组件

[英]reusable angular components

I have a search text box in an angular component that is used to search a list of names.我在 angular 组件中有一个搜索文本框,用于搜索名称列表。 I would like to move this into a reusable component to avoid repeating the same code on multiple pages.我想把它移到一个可重用的组件中,以避免在多个页面上重复相同的代码。 What is the best practice in doing this?这样做的最佳做法是什么? Should I just generate a new component and move the logic there?我应该只生成一个新组件并将逻辑移到那里吗? I know there is a way to perform component interaction with @input decorations but I'm having trouble wrapping my head around how that would work in this scenario.我知道有一种方法可以与 @input 装饰执行组件交互,但我很难理解在这种情况下如何工作。 Here is the following HTML and typescript code:以下是 HTML 和 typescript 代码:

<input [ngModel]="searchStr" (ngModelChange)="employeeSearch($event)" class="form-control mb-3 pl-4" type="text"
        id="employeeName" placeholder="Employee Name" name="employeeName">


  employeeSearch(searchStr: string) {
    this.searchStr = searchStr;
    // some logic
  }

As you already stated, you just create a new component and move the logic there.正如您已经说过的,您只需创建一个新组件并将逻辑移到那里。 Because you want to have the same list of employees in the application, you should introduce a service to your application, which stores/manipulates/etc.因为您希望在应用程序中拥有相同的员工列表,所以您应该向您的应用程序引入一个服务,该服务存储/操作/等。 your list of employees.你的员工名单。 That service then should be used by your component.然后,您的组件应该使用该服务。

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

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