@Output emit value from directive to parent component Angular 4

Miomir Dancevic

I have some upload directive, that is very simple, only problem is that i have to emit value from this directive component to parent component. Does anybody know a simple solution? Thanks in advance. This is my directive for now:

upload-field.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { TooltipModule } from 'ngx-bootstrap/tooltip';

@Component({
  selector: 'app-upload-field',
  templateUrl: './upload-field.component.html',
  styleUrls: ['./upload-field.component.scss']
})
export class UploadFieldComponent implements OnInit {

  @Input() labelName: string;
  @Input() placeHolderValue: string;
  value = '';

  constructor() { }

  ngOnInit() {
  }

  uploadButton(event: any) {
    this.value += event.target.value;
    this.value = this.value.replace(/^.*\\/, '');
  }

}

upload-field.component.html

<input placeholder="{{placeHolderValue}}" disabled="disabled" class="form-control first-input" value="{{value}}" />
<div class="file-upload">
  <span class="btn btn-default btn-lg">{{labelName}}</span>
  <input type="file" class="form-control upload-button" (change)="uploadButton($event)" value="{{value}}" />
</div>

And I use it like this:

<app-upload-field [labelName]="'Blader'" [placeHolderValue]="'Kies bestand'"></app-upload-field>
Paritosh

You can use EventEmitter for this.

Reference: Parent listens for child event

import { Component, Output, EventEmitter } from '@angular/core';
@Component({
  //...your decorator properties
})
export class UploadFieldComponent { 
    @Output() onValueChanged = new EventEmitter<any>();

    uploadButton() {
       this.value += event.target.value;
       this.value = this.value.replace(/^.*\\/, '');
       this.onValueChanged.emit(this.value);
    }

}

In parent component, Template:

<app-upload-field [labelName]="'Blader'" [placeHolderValue]="'Kies bestand'" 
    (onValueChanged)=onValueChanged($event)>
</app-upload-field>

Within component code,

 onValueChanged(value) {
       // value will be the emitted value by the child
 }

The child component exposes an EventEmitter property with which it emits events when something happens. The parent binds to that event property and reacts to those events.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

catch value on event emit from child component in parent component

Angular 4 - How to pass value from parent component to child modules

Angular - emit event from parent component to dynamic child components

Angular 4 call directive method from component

Emit event from Directive to parent element

vuejs emit from a component of a component to the parent

Angular4 @Input() to pass value from parent component to child component

Toggle property in parent Component from Child Component in Angular2 (similar to $emit in AngularJS)

VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on?

Angular Js - emit from $rootScope within directive

Remove Output attribute value from Angular component

Angular Component: how to specified value for output binding function defined in parent?

Update angular directive attribute value from parent controller

angular 4 - call child component function from parent component html

Angular 2: Pass value from attribute directive as a component variable

Angular 5 Update Parent Component value from child Component

Angular to update UI from the child component reflect the value to the parent component

How to correctly emit an event that takes data from a form to the parent component in Angular

Angular 2 how to emit method in component child to component parent

this.$emit not working while passing the value from child to parent component in Vuejs

cant emit multiple value to another component Angular

how to pass a response to parent component from service provider component of angular4 (ionic3), i got undefined value?

Angular directive emitting output, parent not updated

get value from another component angular 4

Emit event from dynamically created child component to parent component

How to print parent value inside child component in angular 4

Angular (4): pass a class object from child to parent component

Check if a form is valid from a parent component using Angular 4

How to pass data from parent to child component in Angular 4