简体   繁体   English

最佳做法是将带有按钮单击绑定角度2的子html标记传递给孩子

[英]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. 来自父级的方法应返回带有方法绑定的按钮html标记,问题在于,当父级传递html标记时,子级未呈现绑定,并且没有任何效果。 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 如果某个功能需要在多个地方使用,而该位置是公共提供程序所在的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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