是否可以从Angular 4中的不同组件访问模板引用变量

是否可以从Angular 4中的不同组件访问模板引用变量,angular,Angular,这是组件A @Component({ selector:'component-a', template: `<div> <div>Component A</div> <div>some other data</div> <ng-template #a_one>A one</ng-template> </

这是组件A

@Component({
selector:'component-a',
template: `<div>
              <div>Component A</div>
              <div>some other data</div>
              <ng-template #a_one>A one</ng-template>
           </div>`
})
export class Component A {
 constructor() {}
}
@组件({
选择器:'component-a',
模板:`
A部分
其他一些数据
一个
`
})
导出类组件A{
构造函数(){}
}
B部分:

@Component({
selector:'component-b',
template: `<div>
              <div>Component B</div>
              <div>some other data</div>
              <div *ngIf='flag; then a_one; else b_one'></div>
              <ng-template #b_one>B one</ng-template>
           </div>`
})
export class Component B {
 @Input() flag: boolean;
 constructor() {}
}
@组件({
选择器:'component-b',
模板:`
B部分
其他一些数据
B一
`
})
出口类组件B{
@Input()标志:布尔;
构造函数(){}
}

我想从组件a访问一个,而不是复制代码。在Angular 4中可能吗?

我不明白什么不清楚?我希望在另一个组件中重用模板,而不是再次创建模板。您不必再次创建模板,web组件就是用于此用途的,您只需在任何需要的地方添加组件即可。您的示例令人困惑,主要是因为您的*ngIf子句。如果我理解得很好,您想在布尔表达式上显示一个模板或另一个模板?您需要传递它,以便其他组件可以重用它。我不明白有什么不清楚?我希望在另一个组件中重用模板,而不是再次创建模板。您不必再次创建模板,web组件就是用于此用途的,您只需在任何需要的地方添加组件即可。您的示例令人困惑,主要是因为您的*ngIf子句。如果我理解得很好,您想在布尔表达式上显示一个模板或另一个基础吗?您需要传递它,以便其他组件可以重用它。