我会根据变量值
更改p-autocomplete组件的样式我有一个切换输入,用于确定我的变量是真还是假
<div class="switch-inner">
<p [ngClass]="{'businessG': !toggle }" class ="toggle-inline ">Business group</p>
<div class="toggle-btn toggle-inline">
<label class="switch">
<input type="checkbox" [(ngModel)]="toggle">
<span class="slider round"></span>
</label>
</div>
<p [ngClass]="{'borrower': toggle }" class="toggle-inline">Borrower</p>
</div>
然后我使用此值在悬停时设置我的建议项目背景的样式。
实际上我使用的是使用:: ngdeep
更改的默认颜色::ng-deep .ui-autocomplete-list-item:hover{
background-color: #24B3C7; // would change this to another color
font-family: 'BNPPSans';
border-radius: 0
}
我会根据切换变量的值更改背景颜色。
这是我的组件html
<div class="container" id ="inputSearchPage">
<div class="search-input col-md-6 col-sm-6 ui-fluid" >
<p-autoComplete [(ngModel)]="text" [suggestions]="results" (completeMethod)="search($event)"
emptyMessage={{noBorrowerResult}}
[minLength]="3"
[size] = "40"
field = "name"
>
<ng-template let-elm pTemplate="item" class="suggestion-item" >
<div >{{elm.name}} ( ID: {{elm.code}} )</div>
<div class="add-button">+</div>
</ng-template>
</p-autoComplete>
</div>
如何根据切换值设置背景颜色? (如果为true,则将::ng-deep .ui-autocomplete-list-item:hover
背景颜色设置为颜色1,否则设置为颜色2)
答案 0 :(得分:1)
您可以使用ngStyle,但目前不支持悬停,因此您必须使用mouseenter和mouseleave解决此问题。
所以在你的html中你可以添加
[ngStyle]="(hover && toggle) ? { 'background-color': 'someColor' } : { 'background-color':
'anotherColor' }" (mouseover)="hover=true" (mouseleave)="hover=false"
答案 1 :(得分:1)
最好的方法是使用hostListeners - &#39; mouseenter&#39;和&#39; mouseleave&#39;