2017-11-20 6 views
0

私はこのコードを持っている:角度5つのgetクラスのコンテキスト

menu.component.ts私

export class menuComponent { 
    menu: object; 
    state: boolean; 

    constructor() { 
     this.state = false; 
     this.menu = [ 
      item: { 
       action: this.action 
      } 
     ] 
    } 

    action(): void { 
     this.state = !this.state; 
    } 
} 

menu.component.html

<ul *ngFor="let item of menu"> 
    <li> 
     <a (click)="item.action()"></a> 
    </li> 
</ul> 

を内部のクラスの関数と属性を使用する必要がありますアクション楽しい例えば、の状態属性。

+1

まずあなたが**コール**機能に必要: '(クリック)=" item.action () "'。次に、アクションが 'this'にバインドされていることを確認する必要があります:' action:()=> this.action() 'または' action:this.action.bind(this) ' –

+1

[コールバック内の正しい\ 'this \ 'にアクセスするには?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback ) –

+0

申し訳ありませんが、私はすでにそれを修正 – Carlos

答えて

1

あなたは状態の機能を持っている理由私は理解していないが、あなたはbindを使用することができますが:

this.menu = [ 
     item: { 
      action: this.action.bind(this) 
     } 
    ]