AngularJS和JSON多行字符串
AngularJS and JSON with multiline string
我已经在JSON文件中组织了我的数据,其中包含多行字符串,在数组中分隔。这样的:
[
{
"name": "1. Some Name",
"id": "1",
"description": [
"Some long text 1 ",
"Some long text 2 "
]
},
{
"name": "2. Some Name",
"id": "2",
"description": [
"Some long text 1 ",
"Some long text 2 "
]
}
]
然后在我的视图中,我想在description中显示文本:
<ion-view view-title="">
<ion-content>
<div class="card">
<div class="item item-text-wrap"
ng-repeat="rule in rules | filter: { id: whichid }">
{{ rule.description }}
</div>
</div>
</ion-content>
我的输出是这样的:
["Some long text 1","Some long text 2"]
如何删除(或过滤)字符'[',' "'和','?
或者如果我使用ng-bind-html="rule.description"指令,我得到:
Some long text 1 ,Some long text 2
基本上这是一个很好的输出,但是它们包含一个逗号','(它在数组中)。
您也可以尝试Array.join()方法。
链接:Array.join ()
在你的情况中:{{rule.description.join (' ') }}
试试
<div class="item item-text-wrap"
ng-repeat="rule in rules | filter: { id: whichid }">
<span ng-repeat="d in rule.description">{{ d }}</span>
</div>
这也给了我很多悲伤。然而,我用一个定制的管道解决了它。做一个管道的研究,但这应该有帮助:
管道/arraytextfix/arraytextfix。ts(管道文件):
import { Pipe, PipeTransform } from '@angular/core';
/**
* Generated class for the ArraytextfixPipe pipe.
*
* See https://angular.io/api/core/Pipe for more info on Angular Pipes.
*/
@Pipe({
name: 'arraytextfix',
})
export class ArraytextfixPipe implements PipeTransform {
/**
* This is a very important pipe, as it removes a joining
* comma, as outlined on this page: https://stackoverflow.com/questions/39557436/angularjs-and-json-with-multiline-string
*/
transform(value) {
value = value.join(' ');
return value
}
}
另一件重要的事情是,将管道文件添加到需要使用它的文件的模块中。
例如:import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ProjectsPage } from './projects';
import { TranslateModule } from '@ngx-translate/core'; // For translations to work
import { ArraytextfixPipe } from "../../pipes/arraytextfix/arraytextfix" // ADD THIS
@NgModule({
declarations: [
ProjectsPage,
ArraytextfixPipe // ADD THIS
],
imports: [
TranslateModule, // For translations to work
IonicPageModule.forChild(ProjectsPage),
],
})
export class ProjectsPageModule {}
然后,(对我来说)你可以用类似的方式处理所有的数据,甚至还可以使用翻译管道:
<p [innerHTML]="'PROJECTS.BODY' | translate | arraytextfix"></p>
这基本上是我的i18n/en。Json数据提要:
{
"PROJECTS":
{
"HEADING": "Projects",
"DESCRIPTION": "A default description",
"BODY": ["bodytext line 1 <p>even has support for a paragraph</p>",
"<p>Works well on line 2</p>",
"line 3"]
}
}
希望对大家有所帮助
相关文章:
- 如何使用JSON字符串中的jQuery填充下拉框
- Json字符串可以'当字符串末尾有“'时,t解码;
- 使用JSON文件中的变量(字符串)填充文本区域
- 将JSON转换为typescript中的字符串数组
- jquery从2个json字符串构建一个复选框表单
- RegEx只匹配JSON字符串中最里面的数组
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 通过字符串获取JSON对象
- 获取JSON并使用Javascript解析为字符串
- 用javascript修复这个JSON对象字符串最干净的方法是什么
- javascript数组到字符串JSON
- 角度拆分字符串 json
- 跨域 AJAX 调用返回字符串 JSON,而不是 JSON 对象
- 如何在js或jQuery中获取长度字符串json
- 字符串JSON引用名称,而不是被引用的对象
- 子字符串JSON键
- 将字符串JSON转换为多维JavaScript数组
- 字符串json具有空值
- 将html字符串(json表示)转换为实际的javascript对象
- PHP 存储字符串 JSON 与 JavaScript 不同