Javascript 如何在模板中显示数据
我是安格拉尔的新手 sample.htmlJavascript 如何在模板中显示数据,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.
<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。这将特别有帮助,因为角度学习曲线是陡峭的。