从HTML文件中的TypeScript(.ts)获取值。

时间:2016-05-12 23:19:15

标签: javascript html typescript angular

我是Typescript和HTML的新手。我正在构建一个Angular2应用程序,我目前有一个类似于此的TypeScript文件:

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变量的方法 - 任何想法?

2 个答案:

答案 0 :(得分:2)

以下是您要找的内容: 链接到工作代码: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';
  };
}

答案 1 :(得分:0)

还有json管道

{{jsonResponse | json}}