将输入模型绑定到数组中的每个对象

Bind input model to every object in array

我有一个简单的 input 像这样:

<input type="text" ng-model="myModel" />

我的控制器中有一个这样的数组:

myApp.controller('MyCtrl', function($scope) {
    $scope.arr = [
        {str: ''},
        {str: ''}
    ];
});

如何将我的 input 模型绑定到 arr 中每个对象的 str 属性?

Fiddle here.

感谢您的帮助。

Use ng-change directive and update the scope using angular.forEach

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
  $scope.arr = [{
    str: ''
  }, {
    str: ''
  }];
  $scope.updateIt = function(myModel) {
    angular.forEach($scope.arr, function(value, key) {
      value.str = myModel;
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  <code>{{arr}}</code>
  <br />
  <br />
  <!-- bind input model to every object 'str' -->
  <input type="text" ng-model="myModel" ng-change='updateIt(myModel)' />
</div>

Fiddle Demo