【问题标题】:angular 2 directive with bootstrap带有引导程序的 Angular 2 指令
【发布时间】:2016-04-10 14:43:17
【问题描述】:

我正在尝试使用引导程序在 Angular 2 中创建一个简单的列表组,我将其放置在 item.component.ts 中,将 ul 放置在 list.components.ts 中但我不知道如何删除角度 2 中的组件指令。

//list.component.ts
<ul class="list-group">
  <my-list *ngFor....> //how to remove this from the view
    //item.component.ts
    <li class="list-group-item">Cras justo odio</li>
    <li class="list-group-item">Dapibus ac facilisis in</li>
    etc.
  </my-list>
</ul>

我不想使用 ng2-bootstrap,只要在浏览器中呈现指令,我的列表组就无法正常工作!

我查看了之前的问题,但找不到任何好的答案如何使这项工作?人们是如何解决这个问题的,我知道 angular1 有 replace:true 来处理这些问题,但在 angular2 中不再。

1.我是否应该在我的 my-list 上添加“css”,使其表现得像 li 并在 div 周围放弃它?但是我必须在我需要使用的每个引导组件上执行此操作!

2.我应该停止使用 item.component.ts 并保留 list.component.ts 中的所有内容来解决此问题吗?

【问题讨论】:

  • 你想要什么而不是“
  • @ThierryTemplier 他想删除它并且只有列表项。
  • @Chrillewoodz 我不认为这是可能的。也就是说,您可以利用属性上的选择器来应用组件...请参阅我的答案。

标签: angular angular2-template angular2-directives


【解决方案1】:

Angular2 不支持 Angular 1 中的 replace: true 功能。

您可以利用选择器中的属性来附加组件:

@Component({
  selector: '[my-component]'
  (...)
})

并以这种方式使用它:

<div my-component>
  <tr>...</tr>
</div>

【讨论】:

  • 我已经在其他问题上看到了这个答案,但我看不出我应该如何使用它? item.component.ts 的目的是我可以在其中添加 li。选择器属性只允许我操作元素属性?
  • 请删除您的帖子,因为它让其他人认为它有效:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2016-09-06
  • 2016-06-23
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 2018-06-23
相关资源
最近更新 更多