【发布时间】:2018-04-16 20:46:19
【问题描述】:
大家好,我制作了一个 Angular 4 应用程序,并且我有我的组件:Contentholder。 Contentholder 只是一些 HTML 和 CSS,用于显示我正在使用的内容持有者。
当我使用<content-holder></content-holder> 时,它可以正常工作并向我显示内容持有者。
但是当我这样使用它时:
<content-holder>
<div>
<h2>{{ translationservice.getExpression("Kiosk.Markforgotten_Text") }}</h2>
<p>{{ translationservice.getExpression("Kiosk.Markforgotten_Subtext") }}</p>
</div>
</content-holder>`
它只是向我显示了 Contentholder,“content-holder”括号之间的所有内容都被抛在了后面。
如何使用 contentholder 组件并将我自己的 HTML 从我正在使用的页面中放入其中?不只是复制和粘贴。这就是我尝试将 contentholder 设为组件的原因。
----------更新---------- ---
当我这样使用它时:
<content-holder>
<ng-content>
<div>
<h2>{{ translationservice.getExpression("Kiosk.Markforgotten_Text") }}</h2>
<p>{{ translationservice.getExpression("Kiosk.Markforgotten_Subtext") }}</p>
</div>
</ng-content>
我得到这个错误:
Uncaught Error: Template parse errors:
<ng-content> element cannot have content. ("
<content-holder>
[ERROR ->]<ng-content>
<div>
<h2>{{ translationservice.getExpression("Kiosk.Markforgotten"): ng:///AppModuleShared/ForgotMarkComponent.html@3:4
at syntaxError (compiler.es5.js:1689)
at TemplateParser.parse (compiler.es5.js:12801)
at JitCompiler._compileTemplate (compiler.es5.js:26846)
at compiler.es5.js:26766
at Set.forEach (<anonymous>)
at JitCompiler._compileComponents (compiler.es5.js:26766)
at compiler.es5.js:26653
at Object.then (compiler.es5.js:1679)
at JitCompiler._compileModuleAndComponents (compiler.es5.js:26652)
at JitCompiler.compileModuleAsync (compiler.es5.js:26581)
【问题讨论】:
-
您是否收到错误消息?
-
不,没有错误!它只是不显示我在 contentholder 括号之间抛出的 HTML
-
这是完全正常和预期的。
将显示 content-holder 组件的模板。不是开始标签和结束标签之间的内容。如果要在组件的模板内显示标签内的内容,则需要在组件的模板内使用 。 -
那是如何工作的?我将我的 div 放在
之间,但我收到一条错误消息,告诉我 ng-content 元素不能有内容 [my div and h2,p] -
@MexLataster 你能说明你在哪里使用
ng-content吗?
标签: angular