Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

sass - Place icon inside search field as a button in Angular

I am trying to place the "search_icon" magnifying glass inside of the search field and code it as a button. This is what I have: enter image description here

This is what I am going for, but I need to make the icon clickable, and this is if it is written inside of the scss file.

enter image description here

Here is my HTML:

      <input *ngIf="!(isMobile$ | async) && (isOnline$ | async)"
      #filterTextInput
      id="filter-text-input"
      class="search-box"
      type="text"
      [value]="itemFilterText$ | async"
      autocomplete="off"
      placeholder="{{'HEADER.SEARCH.PLACEHOLDER' | translate}}"
      attr.aria-label="{{'HEADER.SEARCH.PLACEHOLDER' | translate }}"
      (keyup.enter)="searchForValue(searchString.value)" />
      <mat-icon svgIcon="search_icon"></mat-icon>

Any suggestions?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can do that with some custom CSS styles for the click event, you can simply use the (click) event: HTML

<input *ngIf="!(isMobile$ | async) && (isOnline$ | async)"
      #filterTextInput
      id="filter-text-input"
      class="search-box"
      type="text"
      [value]="itemFilterText$ | async"
      autocomplete="off"
      placeholder="{{'HEADER.SEARCH.PLACEHOLDER' | translate}}"
      attr.aria-label="{{'HEADER.SEARCH.PLACEHOLDER' | translate }}"
      (keyup.enter)="searchForValue(searchString.value)" />
      <mat-icon (click)="doSomething()" svgIcon="search_icon" class="search-icon"></mat-icon>

CSS

.search-icon {
  position: relative;
  right: 32px;
  top:8px;
  width: 24px;
  height:24px;
  font-size: 24px;
  cursor: pointer;
}

Inside your componenet

doSomething() {
    console.log("I am doing something!");
  }

See this working demo

But that's the ugly way of doing it, since you are already using angular material, it is better to stick to the material design guidelines. In your case you are using forms so it would be better to use something like this to align your inputs/icons

<mat-form-field>
  <mat-icon matPrefix (click)="doSomething()">search</mat-icon>
  <input matInput type="search" 
   *ngIf="!(isMobile$ | async) && (isOnline$ | async)"
   #filterTextInput
   id="filter-text-input"
   class="search-input"
   [value]="itemFilterText$ | async"
   autocomplete="off"
   placeholder="{{'HEADER.SEARCH.PLACEHOLDER' | translate}}"
   attr.aria-label="{{'HEADER.SEARCH.PLACEHOLDER' | translate }}"
   (keyup.enter)="searchForValue(searchString.value)"
  >
</mat-form-field>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...