如何将2个模型绑定到一个有角的输入域?

时间:2021-06-04 19:44:02

Is there anyway that I can bind two model values to one input field?

我是否可以将两个模型值绑定到一个输入字段?

Suppose I have input field which I want to be the value of two variables in the scope something like:

假设我有一个输入域我想要成为范围内两个变量的值比如:

<input type="text" model="sn_number; id" > 

4 个解决方案

#1


131  

You cannot, but there are some workarounds.

你不能,但有一些变通方法。

1. Use ngChange to update the other model

<input type="text" 
       ng-model="sn_number" 
       ng-change="id=sn_number"/> 

2. You could watch a model, and when in changes, update another

$scope.$watch('sn_number', function(v){
  $scope.id = v;
});

You would need to watch also for changes in id if you want to keep them in sync.

如果希望保持id的同步,还需要注意id的更改。

Example here

例子

#2


9  

You can bind fields immediately, not only in ng-change, and actually it isn't data binding, its only angular expression

您可以立即绑定字段,不仅是在ng-change中,而且实际上它不是数据绑定,而是它唯一的角度表达式

  <label>Name</label>
  <input type="text" ng-model="name" value="{{name}}"/>

  <label>Key</label>
  <input type="text" ng-model="key" value="{{key=name}}" />

#3


7  

It would make no sense to bind an input to two variables in the model. Binding works both ways, so if the model is updated, the field is updated and vice-versa. If you were to bind to two variables, what would be the single source of truth?

将输入绑定到模型中的两个变量是没有意义的。绑定以两种方式工作,因此,如果模型被更新,则字段被更新,反之亦然。如果你绑定到两个变量,真理的唯一来源是什么?

However you can use ng-change to call a method on the controller which can set two variables when the field changes.

但是,可以使用ng-change调用控制器上的一个方法,该方法可以在字段更改时设置两个变量。

#4


0  

with ng-init

与ng-init

<div ng-controller="ctrl" ng-init="model = { year: '2013', month:'09'}">

or

<div ng-repeat="c in contact" ng-init="likes = { food: 'steak', drink:'coke'}">

#1


131  

You cannot, but there are some workarounds.

你不能,但有一些变通方法。

1. Use ngChange to update the other model

<input type="text" 
       ng-model="sn_number" 
       ng-change="id=sn_number"/> 

2. You could watch a model, and when in changes, update another

$scope.$watch('sn_number', function(v){
  $scope.id = v;
});

You would need to watch also for changes in id if you want to keep them in sync.

如果希望保持id的同步,还需要注意id的更改。

Example here

例子

#2


9  

You can bind fields immediately, not only in ng-change, and actually it isn't data binding, its only angular expression

您可以立即绑定字段,不仅是在ng-change中,而且实际上它不是数据绑定,而是它唯一的角度表达式

  <label>Name</label>
  <input type="text" ng-model="name" value="{{name}}"/>

  <label>Key</label>
  <input type="text" ng-model="key" value="{{key=name}}" />

#3


7  

It would make no sense to bind an input to two variables in the model. Binding works both ways, so if the model is updated, the field is updated and vice-versa. If you were to bind to two variables, what would be the single source of truth?

将输入绑定到模型中的两个变量是没有意义的。绑定以两种方式工作,因此,如果模型被更新,则字段被更新,反之亦然。如果你绑定到两个变量,真理的唯一来源是什么?

However you can use ng-change to call a method on the controller which can set two variables when the field changes.

但是,可以使用ng-change调用控制器上的一个方法,该方法可以在字段更改时设置两个变量。

#4


0  

with ng-init

与ng-init

<div ng-controller="ctrl" ng-init="model = { year: '2013', month:'09'}">

or

<div ng-repeat="c in contact" ng-init="likes = { food: 'steak', drink:'coke'}">