在p标签内添加ul时,Angular模板解析错误

时间:2018-08-31 04:33:58

标签: html angular2-template

我正在开发演示项目,如果没有在P标签内添加UL标签,则可以正常工作。

工作代码:

<p>
<br/> Umesh test help <br/>
</p>
<ul>
<li *ngFor="let user of help2Listdata">
    <a routerLink="/details/{{user.hid}}">{{ user.hid }}</a>

    <ul>
        <li><a href="http://{{ user.name }}">{{ user.name }}</a></li>
        <li>{{ user.ans }}</li>
    </ul>
</li>
</ul> 

无效代码:

<p>
<br/> Umesh test help <br/>

<ul>
    <li *ngFor="let user of help2Listdata">
        <a routerLink="/details/{{user.hid}}">{{ user.hid }}</a>

        <ul>
            <li><a href="http://{{ user.name }}">{{ user.name }}</a></li>
            <li>{{ user.ans }}</li>
        </ul>
    </li>
</ul>
</p>

错误跟踪:

ERROR Error: Uncaught (in promise): Error: Template parse errors:
Unexpected closing tag "p". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
        </li>
    </ul>
[ERROR ->]</p>"): ng:///Help2Module/Help2Component.html@13:0
Error: Template parse errors:
Unexpected closing tag "p". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
        </li>
    </ul>
[ERROR ->]</p>"): ng:///Help2Module/Help2Component.html@13:0
    at syntaxError (compiler.js:486)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer._preparseLoadedTemplate (compiler.js:3222)
    at compiler.js:3202
    at Object.then (compiler.js:475)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer._preParseTemplate (compiler.js:3202)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer.normalizeTemplate (compiler.js:3180)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/esm5/compiler.js.CompileMetadataResolver.loadDirectiveMetadata (compiler.js:14914)
    at compiler.js:34420
    at Array.forEach (<anonymous>)
    at compiler.js:34419
    at syntaxError (compiler.js:486)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer._preparseLoadedTemplate (compiler.js:3222)
    at compiler.js:3202
    at Object.then (compiler.js:475)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer._preParseTemplate (compiler.js:3202)
    at DirectiveNormalizer.push../node_modules/@angular/compiler/esm5/compiler.js.DirectiveNormalizer.normalizeTemplate (compiler.js:3180)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/esm5/compiler.js.CompileMetadataResolver.loadDirectiveMetadata (compiler.js:14914)
    at compiler.js:34420
    at Array.forEach (<anonymous>)
    at compiler.js:34419
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:4751)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)

请让我帮助了解为什么会遇到此问题。当我在检查模式下看到工作代码输出时,看起来很好,但是当更改标签树结构时,它会在浏览器控制台中抛出错误。

1 个答案:

答案 0 :(得分:0)

根据w3c规范,实际上p标签内不允许使用ul标签。这将失败。尝试使用其他标签,例如div或ng-content。

编辑:根据W3C spec,p标签可以全部包含“短语元素”,这只能在以下标签中:

a
em
strong
small
i
b 

See full list...