简体   繁体   English

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

[英]Get value from TypeScript (.ts) in HTML file.

I'm new to Typescript and HTML. 我是Typescript和HTML的新手。 I'm building an Angular2 app and I currently have a TypeScript file that looks like this: 我正在构建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";
    }
}

I'm trying to figure out how to get the value for "jsonResponse" in an HTML file. 我试图弄清楚如何在HTML文件中获取“ jsonResponse”的值。 I've searched but can't find a straightforward way to access the jsonResponse variable - any ideas? 我已经搜索过,但是找不到访问jsonResponse变量的直接方法-有什么想法吗?

Here is what you are looking for: link to working code: https://plnkr.co/edit/Y6TeraRZT2NDnHMRB9B0?p=info 这是您要查找的内容:链接到工作代码: 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}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM