0

I got this problem, I got a web application, and I got a html page, actually, two of them, and now I made a directive (appResizer) that works on both my pages, and in order to improve my code, I want to send a string from the html page to the directive. Can you help me pls?

Here is the code from my 2 pages:

<div appResizer>
...
</div>

Basically, they both have a large div, that I show or hide if needed.

Then I got my directive:

import { Directive, ElementRef, Input, Renderer, OnInit, HostListener, 
Output, EventEmitter } from '@angular/core';

@Directive( {
    selector: '[appResizer]'
} )
export class ResizerDirective implements OnInit { 
    // 
}

I need to somehow send a sting to the directive, as an input or something... Please help.

1 Answer 1

1

Have you tried this:

In the html page:

<div [appResizer]="'yourStringToSend'"

And in the directive:

@Input() appResizer: string;

This is one way to do it.

The other is, to make a global service, add a variable into the service, that shows the string you wanna send to the directive, and then use it like this:

In glolobalService.ts make a variable:

public myStringToSend: string;

In the directive:

import { GlobalService } 'global.service';

And use it normal as you would in other parts of the angular4 environment.

console.log(this.globalService.myStringToSend);

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

omg thx man, it worked, tried the first variant, without the importing of a new service....thx a lot