2016-05-21 12 views
0

マウスのクリック時にdivをハイライトする方法を理解しようとしています。私が(onmouseup)(onmouseclickまたはonmouseClickを認識しない)場合、すべての項目が強調表示されています。以下に、ディレクティブコードを示します。 Here is the Plnkr Codeクリックしてdivを強調表示する角度のハイライト指示

import {Directive, ElementRef} from "@angular/core"; 

@Directive({ 
    selector:'[highlight]', 
    host: { 
    '(mouseup)': 'onMouseUp()', 
    } 

// host: { 
// '(mouseenter)': 'onMouseEnter()', 
// '(mouseleave)': 'onMouseLeave()', 
// } 
}) 
export class Highlight{ 
    private el:ElementRef; 
    constructor(el:ElementRef){ 
    this.el = el; 
    } 


    onMouseEnter(){ 
    console.log(this.el); 
    this.el.nativeElement.style.backgroundColor = 'white'; 
    this.el.nativeElement.style.backgroundColor = '#D1D301'; 
    } 
    onMouseLeave(){ 
    this.el.nativeElement.style.backgroundColor = 'black'; 
    this.el.nativeElement.style.backgroundColor = 'white'; 
    } 

    onMouseUp(){ 
    console.log(this.el); 
    this.el.nativeElement.style.backgroundColor = 'white'; 
    this.el.nativeElement.style.backgroundColor = '#D1D301'; 
    } 
} 
+0

ユーザーが四角をクリックすると、他のすべての人が白に戻って欲しいのですか? – acdcjunior

+0

これはあなたがそれを構造化したやり方をするのは難しいでしょう。ディレクティブはDOMをトラバースし、DOM構造についての前提を設定する必要があります(うまくいかない)。リスト全体に対して1つのディレクティブの代わりに、各アイテムにディレクティブを追加し、マウスがクリックされたときにイベントを送出する(Outputプロパティを使用する)ようにするのがよいでしょう。次に、親コンポーネントはそのリストを反復し、ディレクティブ上のパブリックメソッドを呼び出して、各アイテムのハイライトを解除します。次に、ディレクティブ上でpublicメソッドを呼び出して、そのメソッドを強調表示します。タブの実装と同様です。 –

+0

ng-classを参照してください – WesW

答えて

1

あなたは@ViewChildrenディレクティブを使用して要素のリストを参照し、これに基づいて、現在のselect要素の選択を解除することができます。このリストは、各要素の入力として提供する必要があります。ここで

はサンプルです:

@Directive({ 
    selector:'[highlight]', 
    host: { 
    '(click)': 'select()', 
    } 
}) 
export class Highlight{ 
    private el:ElementRef; 

    constructor(el:ElementRef){ 
    this.el = el; 
    } 

    @Input() 
    elements; 

    select(){ 
    this.elements.forEach(elt => { 
     elt.unselect(); 
    }); 

    this.el.nativeElement.style.backgroundColor = 'white'; 
    this.el.nativeElement.style.backgroundColor = '#D1D301'; 
    } 

    unselect() { 
    this.el.nativeElement.style.backgroundColor = 'black'; 
    this.el.nativeElement.style.backgroundColor = 'white'; 
    } 
} 

そして、このディレクティブを使用する方法:https://plnkr.co/edit/LnOMPv?p=preview:ここ

@Component({ 
    selector: 'itemdisplay', 
    directives:[Highlight] 
    template: ` 
    <div *ngFor="let item of items"> 
     <div style="display:inline-block; height:80px; width: 70px; border:1px solid black;" highlight [elements]="elements"> 
     {{item.name}} 
     </div> 
    </div> 
    ` 
}) 
export class ItemDisplay{ 
    @ViewChildren(Highlight) 
    elements:Highlight[]; 

    public items = [ 
    {id:1, name:"Item1"} 
    {id:2, name:"Item2"} 
    {id:3, name:"Item3"} 
    {id:4, name:"Item4"} 
    {id:5, name:"Item5"}  
    ]; 
} 

は作業plunkrです。

+0

ありがとうございます。私はViewChildrenの仕組みを知らないが、これはまさに私が探していたものである。 ViewChildrenのドキュメントを調べます。ソリューションを改善する方法はありますか?理想的には、私は "ハイライト"を自分の属性として指定したいと思っています。ハイライト属性で[elements] = "elements"を使うことは、何か特別なものです。単なる考えです。 – ATHER

関連する問題