[英]Display JSON object in template angular 2
I have this:我有这个:
stanservice.categoryDetail(this.params.get('id'))
.then((data) => {
this.category = JSON.stringify(data.res.rows[0]);
console.log(JSON.stringify(data.res.rows[0]));
})
.catch((error) => {
console.log("Error message", error.err);
});
The console log returns this:控制台日志返回:
{"id":6,"name":"destiny","note":"nice","type":"income"}
Then I am able to display this.category
in my template as this:然后我可以在我的模板中显示
this.category
如下:
{{ category }}
which returns this返回这个
{"id":6,"name":"destiny","note":"nice","type":"income"}
However, when I try to display the values of the object by doing this但是,当我尝试通过执行此操作来显示对象的值时
{{ category.name }}
I get this error:我收到此错误:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: TypeError: Cannot read property 'name' of undefined in [
{{ category.name }}
in CategorydetailPage@16:18]
ORIGINAL EXCEPTION: TypeError: Cannot read property 'name' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'name' of undefined
at AbstractChangeDetector.ChangeDetector_CategorydetailPage_0.detectChangesInRecordsInternal (viewFactory_CategorydetailPage:67:28)
at AbstractChangeDetector.detectChangesInRecords (http://localhost:8100/build/js/app.bundle.js:13535:18)
at AbstractChangeDetector.runDetectChanges (http://localhost:8100/build/js/app.bundle.js:13512:14)
at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:8100/build/js/app.bundle.js:13612:18)
at AbstractChangeDetector.runDetectChanges (http://localhost:8100/build/js/app.bundle.js:13516:14)
at AbstractChangeDetector.detectChanges (http://localhost:8100/build/js/app.bundle.js:13501:73)
at ChangeDetectorRef_.detectChanges (http://localhost:8100/build/js/app.bundle.js:14402:73)
at ViewController.willEnter (http://localhost:8100/build/js/app.bundle.js:46071:22)
at NavController._postRender (http://localhost:8100/build/js/app.bundle.js:44385:30)
at http://localhost:8100/build/js/app.bundle.js:44333:27
ERROR CONTEXT:
[object Object]
Don't use the this
keyword within the then
function.不要在
then
函数中使用this
关键字。 And don't stringify your JavaScript object.并且不要对您的 JavaScript 对象进行字符串化。 Instead of:
代替:
stanservice.categoryDetail(this.params.get('id'))
.then((data) => {
this.category = JSON.stringify(data.res.rows[0]);
console.log(JSON.stringify(data.res.rows[0]));
})
.catch((error) => {
console.log("Error message", error.err);
});
Try:尝试:
var app = this;
stanservice.categoryDetail(this.params.get('id'))
.then((data) => {
app.category = data.res.rows[0];
console.log(data.res.rows[0]);
})
.catch((error) => {
console.log("Error message", error.err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.