【问题标题】:Angular2 how to pass reference for ngModel bindingAngular2如何为ngModel绑定传递参考
【发布时间】:2016-10-06 04:20:54
【问题描述】:

我写了这个组件:

@Component({
    selector: 'formfield',
    template: `
        <div>
            <label>{{label}}</label>
            <div>
                <input class="form-control" type="text" [(ngModel)]="model">
            </div>
        </div>
`
})
export class Formfield {
    @Input() label: string;
    @Input() model: string;
}

我在这里使用它:

<formfield label="something" model="somevalue"></formfield>

毫不奇怪,输入字段显示字符串“somevalue”。我怎样才能让它保持变量 somevalue 的值?

【问题讨论】:

标签: binding angular web-component


【解决方案1】:

您需要使用以下内容:

<formfield label="something" [model]="someprop"></formfield>

其中someprop 是使用formfield 组件的组件的属性。

例如:

@Component({
  (...)
})
export class SomeComponent {
  someprop:string = 'some value';
}

【讨论】:

  • 这只是一半。输入字段的更改只会影响模型的值,而不会影响 someprop 的值。
  • model 只是一个@Input 属性,我需要类似@InputOutput () model
猜你喜欢
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 2019-04-24
  • 2017-02-05
  • 1970-01-01
  • 2017-07-27
相关资源
最近更新 更多