简体   繁体   中英

Best practice passing to child html tag with button click binding angular 2

So i have the following structure, for the example i just simplify it, there is few rules that i cannot change due to the bootstrap template that i'm using. I have a parent component that using a child component and passing method. The method from the parent should return a button html tag with method binding, the problem is that when the parent pass the html tag, the child is not render the binding and nothing works. I have different parents that pass different tags with different binding, each parent know what to bind, this is demonstrate what i need to do :

Parent:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';


  parent_func() {
    return `<button type="button" (click)="onSubmitParent()">Click Me!</button>`
  }
  onSubmitParent() {
    console.log("parent")
  }
}


<child [test]="parent_func()"></child>



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

@Component({
  selector: 'child',
  templateUrl: './child.html',
  styleUrls: ['./child.css']
})
export class ChildComponenet {

  @Input() public test:any;

}

<h1>
  {{test}}
</h1>

You need to create a service that you can use so you can share the functionality and multiple places.

If a function is needed to be used in more than one place that is where it common provider comes in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM