Angular 6中的可扩展搜索栏

时间:2018-08-17 09:17:39

标签: angular typescript angular5 angular2-forms angular6

我正在使用Angular 6,我想为其下面的代码编写可扩展的搜索栏。

My Code

现在,我的问题是我想在焦点输出时在搜索框中输入值后停止过渡。如果搜索栏为空,则将发生过渡。请帮忙。

2 个答案:

答案 0 :(得分:2)

您必须使用ngClass,我在单击时添加了“填充”类,如果模糊且没有任何价值,则将其删除

<div class="right">
  <form id="search">
    <input #search type="search" placeholder="Search" [ngClass]="{'filled':filled}" 
         (click)="filled=true" (blur)="filled=search.value?true:false"> 
  </form>
</div>

//remove in your css
//#search input[type=search]
//And add (is the same adding .filled)
#search input[type=search].filled
{
  width: 400px;
  padding-left: 32px;
  color: #4a83c0;
  background-color: #fff;
  cursor: auto;

}

更新,关闭按钮就像

<div class="right">
  <form id="search">
    <input #search type="search" [placeholder]="filled?'Search':''" [ngClass]="{'filled':filled}" 
         (click)="filled=!filled" (blur)="filled=search.value?true:false">
         <button class="close" *ngIf="filled" (click)="search.value='';filled=false">X</button>
  </form>
</div>
.close
{
  z-index:10;
  margin-left:-3rem;
  margin-right:1rem;
}

答案 1 :(得分:1)

您可以将条件类放在仅具有非空值的搜索输入中,然后专门定位CSS。像这样:

<input type="search" [class.has-value]="searchValue" placeholder="Search">

input[type=search]:focus, input.has-value {
   ...
}