在模板角度2中显示JSON对象

时间:2016-05-14 18:25:07

标签: javascript json angular

我有这个:

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);
  });

控制台日志返回:

{"id":6,"name":"destiny","note":"nice","type":"income"}

然后我可以在我的模板中显示this.category

{{ category }}

返回此

{"id":6,"name":"destiny","note":"nice","type":"income"}

但是,当我尝试通过执行此操作来显示对象的值时

{{ category.name }}

我收到此错误:

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]

1 个答案:

答案 0 :(得分:1)

不要在library(dplyr) library(tidyr) gather(mydata, var, val, -var1) %>% unite(varV,var, val, sep=".") %>% group_by(var1, varV) %>% tally() %>% spread(var1, n, fill = 0) # varV blue red # <chr> <dbl> <dbl> #1 var2.0 3 10 #2 var2.1 1 1 #3 var3.1 3 6 #4 var3.2 1 3 #5 var3.3 0 2 #6 var4.0 2 6 #7 var4.1 2 5 函数中使用this关键字。并且不要对您的JavaScript对象进行字符串化。而不是:

then

尝试:

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);
});