2016-05-12 89 views
0

我是新來的Typescript和HTML。我建立一個Angular2應用程序,我現在有一個打字稿文件看起來像這樣:從HTML文件中獲取TypeScript(.ts)的值。

import {Http, Headers} from 'angular2/http'; 
import {Component} from 'angular2/core'; 
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/common'; 
import {Router} from 'angular2/router'; 
import {Routes} from '../routes.config'; 

@Component({ 
    selector: 'home', 
    templateUrl: './app/home/home.html', 
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] 
}) 
export class Home { 
    public jsonResponse: string = ""; 

    constructor(private _http: Http) { 
     var thisReference = this; 
     thisReference.getSensors(); 
    } 

    getSensors() { 
     this.jsonResponse = "testResponse"; 
    } 
} 

我試圖找出如何在HTML文件中獲取「jsonResponse」的值。我搜索,但無法找到一個簡單的方法來訪問jsonResponse變量 - 任何想法?

+0

你只是想顯示jsonResponse上的價值頁? – sourdoughdetzel

+0

將此{{jsonResponse}}放入您的html頁面 – Som

回答

1

這裏是你在找什麼: 鏈接到工作代碼:https://plnkr.co/edit/Y6TeraRZT2NDnHMRB9B0?p=info

import {Component} from '@angular/core' 
@Component({ 
    selector: 'my-app', 
    providers: [], 
    template: ` 
    <div> 
     <h2>jsonResponse: {{jsonResponse}}</h2> 

    </div> 
    `, 
    directives: [] 
}) 
export class App { 
    jsonResponse: string; 

    constructor(){ 
    this.getSensors(); 
    } 

    private getSensors() { 
    this.jsonResponse = 'testResponse'; 
    }; 
} 
0

也有json

{{jsonResponse | json}} 
相關問題