37

My code,

  <modal *ngIf='showModal' [imageValue]="imageId"></modal>

My model component,

@Component({
  selector: 'modal',
  templateUrl: './app/modal/modal.component.html',
  providers: [HeaderClass]
})


export class ModalComponent  {
  imageValue:any;

I want to get the value of this 'imageValue' but I dont know how to do it.Can anyone please help me.Thanks.

3 Answers 3

62

If you want to send data to directive then you should try like this:

This is my custom directive, and I am going to share two value from component or HTML to the directive.

test.directive.ts:

import { Directive, ElementRef, Input, OnInit } from '@angular/core';

@Directive({
    selector: '[input-box]'
})

export class TestDirectives implements OnInit {
    @Input() name: string;
    @Input() value: string;
    constructor(private elementRef: ElementRef) {
    }
    ngOnInit() {
        console.log("input-box keys  : ", this.name, this.value);
    }
}

and now your directive has been created and you will have add this directive into your app.module.ts like below:

app.module.ts:

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { TestDirectives } from '../directives/test.directive';


@NgModule({
  declarations: [
    AppComponent,
    TestDirectives
  ],
  imports: [],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

You will have to use your directive in your html and send data to the directive like in below code.

I am sending name and value to the test.directive.ts .

<div input-box [name]="'lightcyan'" [value]="'CYAN'"></div>

or

<div input-box [name]="componentObject.name" [value]="componentObject.value"></div>

Now see the console or use data in the directive accordingly.

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

3 Comments

@subham, Do we need 'ElementRef' in constructor(private elementRef: ElementRef) { } in test-directive.ts above ?
@mahi ElementRef is your reference for the element to which the directive is being applied. It will reference the <div> element in this case, so you can work on it if needed. Most directives will most likely make use of this.
@subham, I was doing exactly what you are doing. Except that I was passing [myLabel]="tanzeel" without single quotes. I saw your answer and corrected my mistake with [myLabel]="'tanzeel'". thanks bhai :-)
7

This is an Example how you can pass value to a Directive

Directive

    import {Directive, Input, HostListener} from '@angular/core';

    @Directive({
      selector: '[appConfirm]'
    })
    export class ConfirmDirective {

      //we can also add the attribute directive liek this [appconfirm] if the input in the directive has same name as appConfirm like
      //@Input() appConfirm:string; and then in component button we can use the directive like
      //<button type="button" [appConfirm] = "Rahul">Click to Send to Directive</button>
      @Input() value:string;

      @HostListener('click',['$event'])
      confirm(){
          const confirmed = window.confirm("Are you Sure ?");
          if(confirmed){
            window.alert("This is Value ["+this.value+"] Passed by the Component to the Directive")
          }
      }

  constructor() { }

}

ComponentTemplate.html

<button type="button" appConfirm value = "Rahul">Click to Send to Directive</button>

For more info look this repo https://github.com/rahulrsingh09/AngularConcepts/tree/master/src/app/directives

Comments

2

Use @input and pass value from parent component, where this component was used like [imgval]="val"

2 Comments

Hi Remya.J,I did like this but it showing error @Input() imageValue:any;
Have you included input from @angular/core?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.