Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method defined in an Angular 2 directive

I am new to Angular 2, I have injected directive in current Component

@Component({
    selector: 'select-me',
    templateUrl: 'app/template.html',
    directives: [BackgroundChange]    // I want to access this same instance to make the dynamic changes
})

export class PageComponent {
 constructor(private backgroundChange:BackgroundChange) {
// I guess this will create new instance of BackgroundChange
}

}

I want to call methods availabe in BackgroundChange to do some changes dynamically from PageComponent

like image 908
prabhatojha Avatar asked Mar 02 '26 14:03

prabhatojha


1 Answers

Directives are standalone and should be used in your HTML template - not injected into your PageComponent... aka Directives are not injectable.

The BackgroundChange directive will have access to the HTML element it is attached to in your template - and should do its manipulating within.

Angular docs: https://angular.io/docs/ts/latest/guide/attribute-directives.html#!#apply-directive

like image 177
Sherman Szeto Avatar answered Mar 06 '26 14:03

Sherman Szeto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!