javascript - Angular [$插值 :interr] error for applied filter

标签 javascript angularjs json angular-filters

嗨,我有一个过滤器,可以将 \n 替换为\n\n,当我使用此过滤器处理 json 数据时,我收到错误消息 Can't interpolate: {{ post.description |nlToArray }} 类型错误:无法读取未定义的属性“替换”

我的过滤器

sampleApp.filter('nlToArray', function() {
  return function(text) {
      return text.replace(/\n/g, '/\n\n');
  };
});

显示从json api获取的数据

hmtl

<p class="blog-post-title">{{ post.title }}</p>
<p class="blog-post-meta"><i class="fa fa-clock-o">&nbsp {{ post.pub_date|date  }}</i> </p>
<span style="white-space: pre-line">{{ post.description|nlToArray }}</span>
<br />

我的json数据

[{"id":1,"title":"Israel’s desert city of Beersheba is turning into a cybertech oasis","description":"morphing into a tech oasis.\r\nThe military’s massive relocation of its prestigious technology units.\r\nBeersheba has all of the ingredients of a vibrant security technology ecosystem, \r\n“All in all, projections are that 20,000-30,000 \r\nThe commercial sector has teamed up  cyber attacks.","pub_date":"2016-03-20T10:48:19.394643Z"},{"id":2,"title":"These are testing times: mavericks vs. ice people","description":"One of my earliest engineering jobs, before I fled hardware in favor of the (relative). \r\nThe practice of engineering soon teaches one that, .\r\nSo what do we do? We practice defense in depth. We follow the robustness principle. We “always code as \r\n…Yeah, well, that’s the idea. For my day job at HappyFunCorp I do a lot of interviews, and almost every junior develope.\r\nI don’t necessarily blame them. You can make  go.","pub_date":"2016-03-20T10:50:07.965930Z"}]

更清楚一点,json中的描述是这样的

morphing into a tech oasis.\r\nThe military’s massive relocation of its prestigious technology units.\r\nBeersheba has all of the ingredients of a vibrant security technology ecosystem, \r\n“All in all, projections are that 20,000-30,000 \r\nThe commercial sector has teamed up  cyber attacks.

可能的解决方案是什么......提前致谢

最佳答案

通过替换使代码空安全

sampleApp.filter('nlToArray', function() {
  return function(text) {
      return text.replace(/\n/g, '/\n\n');
  };
});

sampleApp.filter('nlToArray', function() {
  return function(text) {
      if (text) 
        return text.replace(/\n/g, '/\n\n');
      else
        return text;
  };
});

关于javascript - Angular [$插值 :interr] error for applied filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36146202/

相关文章:

javascript - map 如何使用 js 处理 (string,arrayList)?

javascript - 为什么在调用有效函数时会出现类型错误? | Angular 2

javascript - JQuery .replaceWith 函数仅提供原始 html

javascript - 显示的空白页面 - Angular JS

javascript - 为什么 angularjs 插件无缘无故地使用 $timeout

angularjs - 如何在 popover 中使用 AngualarJs ng-grid 或 ui-calendar

javascript - Three.js 一个场景中的多个纹理和图像

java - 如何在 Java 中将一个 Json 节点插入到另一个 Json 节点中?

C# JavaScriptSerializer 序列化接口(interface)中不存在但存在于原始对象中的字段

json - 如何在 XE4 中使用 TFDJSONDataSets 或类似的?