【发布时间】:2017-06-05 16:32:41
【问题描述】:
我有一个关于 angular2 的问题。我正在创建一些组件并希望拥有这样的东西:
这是我的 DogComponent 类:
@Component({
selector: "dog",
template: "dog.template.html"
})
class DogComponent
{
@Input() image: string;
}
还有dog.template.html中的模板:
<div>
<!-- Content of <top> should go here -->
<img class="after" src="dogs/{{image}}" />
<!-- Content of <bottom> should go here -->
</div>
当我使用 DogComponent 时,它应该使用传递的 src 创建 img-tag,还可以查看图像前后的其他 HTML 部分。
所以最后,如果我写这段代码:
<dog image="garry.png">
<top>
<h1>This is Garry!</h1>
</top>
<bottom>
<span>He is my favorite dog!</span>
</bottom>
</dog>
应该渲染成这样:
<dog>
<div>
<h1>This is Garry!</h1>
<img src="dog.png" />
<span>He is my favorite dog!</span>
</div>
</dog>
有人回答我的问题吗?
那就太好了!
编辑:
感谢您的建议,所以现在我更新了我的 sn-ps 并添加了 DogListComponent。如果我在应用程序的某处使用标签dog-list,则应查看最后一个 sn-p(浏览器结果)。希望现在更清楚一点。
dog.component.ts
@Component({
selector: "dog",
templateUrl: "dog.template.html"
})
class DogComponent
{
@Input() image: string;
}
dog.template.html
<div>
<!-- Content of <top> should go here -->
<img class="after" src="dogs/{{image}}" />
<!-- Content of <bottom> should go here -->
</div>
dog_list.component.ts
@Component({
selector: "dog-list",
templateUrl: "dog-list.template.html"
})
class DogListComponent
{
}
dog-list.template.html
<dog image="garry.png">
<top>
<h1>This is Garry!</h1>
</top>
<bottom>
<span>He is my favorite dog!</span>
</bottom>
</dog>
<dog image="linda.png">
<top>
<h1>My little Linda :)</h1>
</top>
<bottom>
<span>She is my cutest dog!</span>
</bottom>
</dog>
<dog image="rex.png">
<top>
<h1>And here is Rex!</h1>
</top>
<bottom>
<span>DANGEROUS!</span>
</bottom>
</dog>
浏览器结果:
<dog-list>
<dog image="garry.png">
<top>
<h1>This is Garry!</h1>
</top>
<bottom>
<span>He is my favorite dog!</span>
</bottom>
</dog>
<dog image="linda.png">
<top>
<h1>My little Linda :)</h1>
</top>
<bottom>
<span>She is my cutest dog!</span>
</bottom>
</dog>
<dog image="rex.png">
<top>
<h1>And here is Rex!</h1>
</top>
<bottom>
<span>DANGEROUS!</span>
</bottom>
</dog>
<dog-list>
【问题讨论】:
-
你的意思是它应该在你正在使用的编辑器/IDE中显示它吗?或者通过 DOM - 如果您检查它,它会执行此操作。也是 templateUrl 不是模板
-
它应该在运行应用程序时显示在编译/渲染的 DOM 中。我试图实现,
dog.template.html中的注释<!-- Content of <top> should go here -->被dog-list.template.html中指定的标签<top><h1>This is Garry!</h1></top>替换。
标签: html angularjs angular angular-components