将细节提取到模型视图angularjs中

时间:2017-12-08 11:33:31

标签: javascript angularjs

$scope.orderDetails = [
        {
            "id":"115584",
            "user_id":"78937",
            "your_sys_global":null,
            "created_at":"2017-12-08 05:03:20"
        },
        {
            "id":"115585",
            "user_id":"78936",
            "your_sys_global":"MicSamse",
            "created_at":"2017-12-08 05:03:21"
        },
        {
            "id":"115586",
            "user_id":"78938",
            "your_sys_global":null,
            "created_at":"2017-12-08 05:03:52"
        }
    ];

<div ng-repeat="user in profiles track by $index">
    <input
    type="text"
    name="your_sys_global_{{user.user_id}}"
    ng-model="orderDetails[user.user_id].your_sys_global"
    ng-change="saveOrder(user, orderDetails[user.user_id])"
    >
</div>

详细信息未提取到文本框中,请指导原因

1 个答案:

答案 0 :(得分:0)

问题出在orderDetails[user.user_id],您的$scope.orderDetails是一个数组,您需要索引来更新该数组中的值。

为什么您使用ng-change,如果设置正确,ng-model本身会更新$scope.orderDetails

$scope

中添加以下方法
$scope.getUserOrder = function(user) {
  return $scope.orderDetails.filter(function(order) {
    return order.user_id === user.user_id;
  })[0]
}

并修改您的标记以使用此method

<div ng-repeat="user in profiles track by $index">
    <input
    type="text"
    name="your_sys_global_{{user.user_id}}"
    ng-model="getUserOrder(user).your_sys_global"
    >
</div>

可能会有一些拼写错误,请不要介意。