Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在模板中显示数据_Javascript_Typescript_Angular2 Template_Angular2 Forms - Fatal编程技术网

Javascript 如何在模板中显示数据

Javascript 如何在模板中显示数据,javascript,typescript,angular2-template,angular2-forms,Javascript,Typescript,Angular2 Template,Angular2 Forms,我是安格拉尔的新手 sample.html <label>Name:</label> <input type="text" [(ngModel)]="yourName" placeholder="Enter a name here"> <button (click)="sendData(yourName)">send data</button> <p>{{receivedData}}</p> sample.

我是安格拉尔的新手

sample.html

<label>Name:</label>
<input type="text" [(ngModel)]="yourName" placeholder="Enter a name here">


<button (click)="sendData(yourName)">send data</button>
<p>{{receivedData}}</p>

sample.ts

import {Component} from 'angular2/core';

@Component({
selector: 'hello-world',
templateUrl: 'src/hello_world.html'
})
export class HelloWorld {
yourName: string = '';
receviedData : string = "";
console.log(receivedData);
sendData(yourName) : void{
console.log("sent",yourName);
receivedData = yourName;

你就快到了。您需要在组件ts文件中进行更改。您正在使用以下内容:

receivedData = yourName;
this.receivedData = yourName;
但是,您需要使用以下命令:

receivedData = yourName;
this.receivedData = yourName;
第一种方法尝试将值赋给局部变量,但答案是将值赋给全局变量


希望这有帮助。

不客气。很高兴我能帮忙。如果我可以建议的话,试着像VS代码一样使用IDE。这将特别有帮助,因为角度学习曲线是陡峭的。