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

Get value from TypeScript (.ts) in HTML file.

本文关键字:ts 获取 TypeScript HTML 文件      更新时间:2023-09-26

我是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变量的简单方法——有什么想法吗?

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

还有json管道

{{jsonResponse | json}}