javascript - 只是页面工作中的 ng-bind-html 之一

标签 javascript angularjs bind

我编写 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/

相关文章:

javascript - 对来自两个对象数组的数据求和

javascript - 用两种不同的方式编写函数 : one way results in a TypeError and the other does not. 为什么?

angularjs - AngularJS从后端获取当前用户-错误拦截器

performance - wxPython FloatCanvas 事件绑定(bind)

javascript - 确定字符串是对本地文件还是外部文件的引用

javascript - 使用 Fixture Adapter 将 js 对象切换到 Ember Data 模型并不断收到 "Cannot call method ' findAll' of undefined"错误

javascript - 即使 if 语句为 true,else 条件也始终被执行

angularjs - 如何在 AngularJS HTML 模板中存储变量?

c++ - Boost ASIO async_accept 编译失败

java.net.BindException : Address already in use: JVM_Bind BUT NO port using it