我编写 angularjs 应用程序并拥有这段代码,但只有第一个 html-bind-html 对我有用
<div ng-bind-html='newsTitle'>
<div ng-bind-html='newsDetail'></div>
</div>
当我像这样更改优先级时:
<div ng-bind-html='newsDetail'>
<div ng-bind-html='newsTitle'></div>
</div>
它显示 newsDetail 值。 每页可以声明多少个 ng-bind-html?为什么第二个值不显示?
最佳答案
我想我理解你的问题。
<div ng-bind-html='newsTitle'> <!-- This will replace the content of the div with $scope.newsTitle -->
<div ng-bind-html='newsDetail'> <!-- So this won't appear, because it has been removed by ng-bind-html='newsTitle' -->
</div>
</div>
查看我在代码中的注释。因此,如果您将 newsDetails 放在第一位,则绑定(bind)的 HTML ($scope.newsDetail) 也将替换当前内容。
总之,ng-bind-html 用您提供的绑定(bind) HTML 替换元素的当前内容。因此您不应该将 HTML 放入这些元素中。
你只需要做这样的事情:
<div class="news">
<div ng-bind-html='newsTitle'></div>
<div ng-bind-html='newsDetail'></div>
</div>
有关 ngBindHtml 指令的一些文档:https://docs.angularjs.org/api/ng/directive/ngBindHtml
关于javascript - 只是页面工作中的 ng-bind-html 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777518/