简体   繁体   English

Angular2双向数据绑定不起作用

[英]Angular2 Two-Way Data Binding not working

Here is an example code: 这是示例代码:

var SampleComponent7 = ng.core.Component({
    selector: "sampleseven",
    template: "<input [(value)]='name' /><input (click)='clicked()' value='Click here' type='submit' />"
}).Class({
    constructor: function(){
        this.name = "";
    },
    clicked: function(){
        alert(this.name);
    }
})

The alert box seems to always print empty string even if I enter some value in the input box. 即使我在输入框中输入了一些值,警报框似乎也总是打印空字符串。 Am I missing something? 我想念什么吗?

I would use the following with [(ngModel)] instead of [(value)] : 我会在[(ngModel)]而不是[(value)]使用以下内容:

var SampleComponent7 = ng.core.Component({
  selector: 'sampleseven',
  template: '<input [(ngModel)]="name" /><input (click)="clicked()" value="Click here" type="submit" />'
}).Class({
  constructor: function(){
    this.name = "";
  },
  clicked: function(){
    alert(this.name);
  }
});

See this plunkr: https://plnkr.co/edit/PevjoFUasncybPGnVkU0?p=preview . 请参阅以下代码: https ://plnkr.co/edit/PevjoFUasncybPGnVkU0 ? p = preview。

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

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