我从数组列表中获得了对象字符串数组。我使用 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/