1

These are my code

child.component.ts

import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core';
import { DomSanitizer } from "@angular/platform-browser";

@Component({
  selector: 'child-component',
  templateUrl: './child.component.html',
})
export class ChildComponent implements OnInit {
  @Input() dataFromParent: String;
  @Output() sendDataToParent = new EventEmitter<string>();
  htmldata:any;

  constructor(private sanitizer: DomSanitizer){

  }

  ngOnInit() {
    this.htmldata = '<input type="text" #data><button (click)="_sendDataToParent(data.value)">Send event to parent</button>';

    console.log('This is data from parents', this.dataFromParent);
    console.log('this.htmldata', this.htmldata);
  }

  _sendDataToParent(data:string) {
    console.log('you clicked');
    this.sendDataToParent.emit(data);
  }

  makeSanitize(str: any)
  {
    return this.sanitizer.bypassSecurityTrustHtml(str);
  }
}

child.component.html

<h2>Child Component</h2>
<p>{{dataFromParent}}</p>
<span [innerHTML]="makeSanitize(htmldata)"></span>

app.component.html

<h2>Parent Component</h2>
<p>
    {{dataFromChild}}
</p>

<hr>
<hr>
<hr>

<child-component 
  [dataFromParent]="'This is data sent from parent'"
  (sendDataToParent)="eventFromChild($event)">
</child-component>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 6';
  dataFromChild;
  eventFromChild(data) {
    console.log(data);
    this.dataFromChild = data;
  }
}

I planned to generate htmldata dynamically.

htmldata has innerhtml values in its child component. Innerhtml also have Click event which is not working.

I want to fill the input and click the Send event to parent button. But the event was not triggered.

Dear developers please help me

1
  • Friends please anyone guide me Commented Jan 21, 2020 at 12:10

1 Answer 1

1

You are coding "angular" in a string value and then putting it into the DOM. The angular code needs to be compiled to be transformed in real html/js code

Thus you need to start by writing "onclick" instead of "(click)" But then you will have other error like "_sendDataToParent is not defined" because you would need that function a "script" tag

See here to test https://stackblitz.com/edit/angular-4s9fzs

In the end, producing angular code in a string and then putting it into the DOM is probably something you don't want to do

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

3 Comments

Thanks for your reply. I am getting error like this when I type input text and click send to parent button Error occurred (1:1) _sendDataToParent is not defined
Yes as stated in my comment, you would need other changes to make it work because it's not angular anymore but plain html Answer : "But then you will have other error like "_sendDataToParent is not defined" because you would need that function in a "script" tag"
Sorry, It is not what i am exactly seeking for. I want pass some arguments via this function also. Is there anyway to make it work using "click" instead of "onclick"

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.