2017-02-18 14 views
5

का उपयोग कर होवर गुण सेट करें मैं [ngStyle] का उपयोग कर होवर प्रॉपर्टी स्टेटस सेट करने का प्रयास कर रहा हूं। निम्नलिखित केवल सामान्य राज्य रंग सेट करता है। जब मैं बटन पर माउस करता हूं, तो बटन दबाए गए रंगों में अपेक्षित रूप से परिवर्तित नहीं होता है।कोणीय 2: ngStyle

<button (click)="logout()" 
        class="btn register-button" 
        [ngStyle]=" hover ? {'color': theme.logoutButtonColorPressed, 
           'border-color': theme.logoutButtonBorderColorPressed, 
           'background-color': theme.logoutButtonBackgroundColorPressed } : 

           {'color': theme.logoutButtonColorNormal, 
           'border-color': theme.logoutButtonBorderColorNormal, 
           'background-color': theme.logoutButtonBackgroundColorNormal }">Logout</button> 
+0

क्या है 'मंडराना 'यहां' [ngStyle] = "होवर {' ? –

+0

यह स्पष्ट नहीं है कि आप क्या चाहते हैं। यदि आप होवर पर शैलियों को स्विच करना चाहते हैं, तो बटन '<बटन (माउसओवर) = "होवर = सच्चा" (माउसलेव) = "होवर = झूठा" = ... –

+0

बटन में निम्नलिखित जोड़ें, मैं सीएसएस को दोहराने की कोशिश कर रहा हूं .. .. .logout-container बटन: होवर { } होवर राज्य के लिए बटन सेट करने की उम्मीद कर रहा है। मैं इन्हें मैन्युअल रूप से का उपयोग कर सेट कर सकता हूं। लॉगआउट-कंटेनर बटन: होवर { रंग: #FFFFFF! महत्वपूर्ण; पृष्ठभूमि रंग: आरजीबीए (0, 0, 0, 0.00)! महत्वपूर्ण; सीमा-रंग: # एफएफएफएफएफएफ! महत्वपूर्ण; } – user2182570

उत्तर

0

:hover एक छद्म वर्ग है, यह style का उपयोग कर जोड़ा नहीं जा सकता। आपको सीएसएस और ngClass या [class.xxxx]=".." का उपयोग करना चाहिए।

+0

आपको क्यों लगता है कि वह 'होवर' सेट करना चाहता है? मैं सोच रहा था कि वह बटन बदलना चाहता है शैलियों –

+0

बटन में 2 से अधिक राज्य हैं: सामान्य, होवर, केंद्रित, अक्षम, सक्रिय। वर्तमान दृष्टिकोण अंत में 'ngStyle' विफल हो जाएगा। – kemsky

6

आप हॉवर पर शैलियों स्विच करना चाहते हैं, तो आप ngstyle बदलकर अलग-अलग बटन का चयन करने की जरूरत में हैं, तो बटन

<button (mouseover)="hover=true" (mouseleave)="hover=false"... 
1

के लिए निम्न जोड़ते हैं, यह मैं क्या किया है।

btn.component.html

<div *ngIf="socketData && socketData.status === 'ok'"> 
    <div *ngFor="let button of socketData.message; let i = index" 
     [ngStyle]="hovered === i ? pressedStyle(button) : buttonStyle(button)" 
     (mouseover)="hovered = i" 
     (mouseout)="hovered = -1" 
     (click)="reqTicket(button.id)"> 

     {{button.someImportantData}} - {{button.yetMoreImportantData}} 
    </div> 
</div> 

btn.component.ts

export class ButtonComponent implements OnInit { 
    style; 
    hovered; 

    ... 

    private buttonStyle(button) { 
     let btnType = this.setBtnType(button); 

     this.style = { 
      'color': '#' + button.textColor, 
      'font-size': button.textSize + 'px', 
      'background-color': btnType.background 
     }; 
     return this.style; 
    } 

    private pressedStyle(button) { 
     let pressed = this.setBtnType(button, this.hovered); 
     this.style['background-color'] = pressed.background; 

     return this.style; 
    } 

    private setBtnType(button, hovered?) { 
     let type = 'normal'; 
     let btn = { 
      normal: { 
       background: '#' + button.backColor, 
      }, 
      pressed: { 
       background: '#' + button.backColor, 

      } 
     } 

     if(hovered > -1) type = 'pressed'; 

     return { 
      border: btn[type].width + ' ' + btn[type].color + ' ' + 'solid', 
      background: btn[type].background 
     }; 
    } 

}

आशा इस मदद करता है किसी को :)