如何在angularjs中观看ng-model的数组值我创建了小提琴,但无法正常工作,请看一下并提出建议。

谢谢

<div ng-app ng-controller="ParentCtrl">
    <div ng-controller="ChildCtrl">
        <table>
            <tr ng-repeat="user in profiles track by $index">
                 <td>
                    <input
                      type="text"
                      name="parktime_{{user.user_id}}"
                      ng-model="orderItems[user.user_id].parktime"
                      >
                 </td>
            </tr>
        </table>
        </div>
</div>



 function ParentCtrl($scope) {
    }

    function ChildCtrl($scope) {
        $scope.parkOptions = {};
        $scope.profiles = [
            {user_id: '01', park_name: 'England Park'},
            {user_id: '02', park_name: 'France Park'}
        ];
 $scope.orderItems = {};
$scope.orderItems.parktime = "Test";

        $scope.$watch('orderItems[profiles.user_id].parktime', function(newVal, oldVal){
            console.log(newVal);
        })

    }


http://jsfiddle.net/w5vskLfc/59/

最佳答案

我建议您改用ng-change。您不能以这种方式使用监视,而是监视单个属性。

我还更新了您的模型以使用ng-init。使用时应注意这一点,但是在您的情况下,需要在orderItems对象的每个位置定义一个键。如果您将始终静态分配它们,我建议对其进行一次迭代并以这种方式创建密钥。

的HTML

      <input type="text" name="parktime_{{user.user_id}}"
      ng-init="orderItems[user.user_id] = {}"
      ng-model="orderItems[user.user_id].parktime"
      ng-change="onChange(user.user_id)">


JS

   $scope.onChange = function(items) {
        console.log(items);
   }

关于javascript - 观看angularjs中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47751975/

10-09 14:07