且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 2 个模型绑定到 Angular 中的一个输入字段?

更新时间:2022-06-04 02:45:39

您不能,但有一些解决方法.

You cannot, but there are some workarounds.

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

2.您可以观看一个模型,并在发生变化时更新另一个

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

如果您想让它们保持同步,您还需要注意 id 中的变化.

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

此处示例