答案 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 {
...
}