javascript - ng 重复 json 解析显示空字符串

标签 javascript angularjs json pug

我从数组列表中获得了对象字符串数组。我使用 JSON.parse 对象的方法将对象字符串转换为对象,但它在 ng-repeat 中显示为空。

Jade

.item.col-md-4(ng-repeat='p in searchData | filter: paginate | orderBy: sortKey ')
   // `p` is a object of string, for example "{id:2, name:'abel'}";
   - var property = JSON.parse("{{ p }}"); // Error at this line
   +AddPropertyCard(property)

搜索数据

[{id:0, name:"abel"},{id:1, name:"julia"}]

错误

var property = JSON.parse("");

Unexpected token { in JSON at position 1

最佳答案

更新 1:

变量“p”似乎是对象。

您必须使用点符号来访问它。 例如 {{p.id}}{{p.name}}。这将显示各自的值。

<小时/>

JSON.parse 需要对象字符串才能解析它。

// Lets create a simple Object in javascript
var notStringObj = {
    "name": "John",
    "age": 30,
    "city": "New York"
};
console.log("JavaScript Object", notStringObj);

// Lets stringify (Convert in string) the object
var stringObj = JSON.stringify(notStringObj);
console.log("JavaScript Stringified Object", stringObj);

// Following is code to decode this object

// This will give you result you are expecting i.e. JavaScript Object
var obj1 = JSON.parse(stringObj);

// This will throw error - Unexpected token { in JSON at position 1
// Because this was plain object not a string
// JSON.parse expects object string in order to Parse it
var obj2 = JSON.parse(notStringObj)

关于javascript - ng 重复 json 解析显示空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46066359/

相关文章:

javascript - 该函数是否执行: jQuery (".class"). function();如果在该页面上找不到该类?

javascript - Angular ng-show、ng-hide 和 ng-if 未按预期工作

angularjs - 如何存储用户名并使其随处可见?

javascript - 得到“未定义ReferenceError”

javascript - 从外部 json 添加标记到谷歌地图?

jquery - json inArray 返回总是 -1 jquery

javascript - 如何使用 JavaScript 覆盖 <p> 标签中的文本

javascript - 如何水平滚动网页?

javascript - API Fetch ES6 中的注册函数

angularjs - setTimeout NodeJS性能