简体   繁体   中英

Angular2 hostlistener

How do you get HostListener working in good old ES6. Currently have...

import { Directive, HostListener }                from 'angular2/core';

@Directive({
    selector: 'focusable',
    hostListeners: {
        'focus' : 'focus()',
        'click' : 'click()'
    }
})
export class FocusableComponent {
    constructor(){
        console.log('constructed');
    }

    focus() {
        console.log('Im focused');
    }

    click() {
        console.log('ive been clicked');
    }
}

From what i could make out from the docs... this seems to be how people have done it, but not working for me!

You should do this way:

@Directive({
  selector: '[focusable]',
  host: {
    '(focus)' : 'focus()',
    '(click)' : 'click()'
  }
})
export class FocusableComponent {
  constructor(){
    console.log('constructed');
  }

  focus() {
    console.log('Im focused');
  }

  click() {
    console.log('ive been clicked');
  }
}

See this plunkr: https://plnkr.co/edit/pBxcqP25JTLTzKY2AwyP?p=preview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM