Angular2双向数据绑定无法正常工作

时间:2016-04-26 12:44:27

标签: angular angular2-template angular2-forms

以下是一个示例代码:

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);
    }
})

即使我在输入框中输入了一些值,警告框似乎总是打印空字符串。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我会将以下内容与[(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);
  }
});

请参阅此plunkr:https://plnkr.co/edit/PevjoFUasncybPGnVkU0?p=preview