我的angular 2对象打印在控制台上,但未从模板打印单个属性

时间:2017-05-26 08:53:06

标签: javascript angular

这是我的模板和组件代码

模板:

View.OnClickListener mOnClickListener = new View.OnClickListener() {
    public void onClick(View v) {
        System.out.println("ACTIVITY mOnClickListener");
        //final Context context = RatesWidgetConfigureActivity.this;

        SharedPreferences.Editor editor = getSharedPreferences(ConfigData.WIDGET_SHARED_PREF_NAME, MODE_PRIVATE).edit();

        editor.putString("widget_currency_1", spCurrencyFirst.getSelectedItem().toString());
        editor.putString("widget_currency_2", spCurrencySecond.getSelectedItem().toString());
        editor.putString("widget_currency_3", spCurrencyThird.getSelectedItem().toString());
        editor.commit();

        // It is the responsibility of the configuration activity to update the app widget

        InAppProperties.getInstance().LoadingRates = true;
        RatesWidget.isRefresh = true;


        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        RatesWidget.updateAppWidget(getApplicationContext(), appWidgetManager, mAppWidgetId);


        // Make sure we pass back the original appWidgetId
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();
    }
};

组件代码:

<div *ngIf="newJobCreated">
        <h3>Name: {{newJob.name}}</h3>
        <h3>Job: {{newJob.job}}</h3>
        <h3>ID: {{newJob.id}}</h3>
        <h3>Created At: {{newJob.createdAt}}</h3>
</div>

当我执行console.log(this.newJob)时,我的控制台会打印对象。就像是 {&#34; name&#34;:&#34; Vinod&#34;,&#34; job&#34;:&#34; Gandhi&#34;,&#34; id&#34;:&#34 ; 4&#34;&#34; createdAt&#34;:&#34; 2017-05-26T08:43:21.476Z&#34;}

但是如果你看一下我的模板,就不会打印出那些单独的值。

请帮忙。

1 个答案:

答案 0 :(得分:1)

您正在执行以下操作时获取响应的字符串值:

this.newJob = data._body

你应该得到JSON,所以快速,简单的解决方法是:

this.newJob = data.json()

我会在服务中使用map

服务:

postJson(....) {
  return this.http.(...)
    .map(res => res.json())
}

然后在组件

.subscribe(data => {
   this.newJob = data;
   this.newJobCreated = true;
})