S
S
Sergey2017-04-07 12:23:14
Angular
Sergey, 2017-04-07 12:23:14

Event handling in angular2 controller?

The guys have an object passed to the component. It is necessary to hang event handling on this object. Can this be done in the controller and not in the template? For example, how to move the click to the controller?
<div #test (click)="test()"></div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
ozknemoy, 2017-04-07
@ozknemoy

like this

@Directive({
    selector: 'blurFocus',
    //при focus на элемент применяется класс focus
    host: {
        '(focus)': 'setInputFocus(true)',
        '(blur)': 'setInputFocus(false)'
    }
})

and inside the class the method itself
setInputFocus(isSet: boolean): void {
        this.renderer.setElementClass(this.elementRef.nativeElement.parentElement, 'focus', isSet);
    }

or hang a listener on the Observable element (here is a listener for entering data into the input)
this.input$ = Observable
            .fromEvent(this._el.nativeElement, 'input')
            .debounceTime(500);
        this.input$.subscribe(event=>  this._check(event))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question