javascript - AngularJS:使用特定键以模式更新 JSON

标签 javascript angularjs json

使用特定键更新 View 中的 json 的最佳做法是什么。 就我而言,我想将反馈从“未回答”更新为“已回答”。

[
  {
    "id": "34",
    "mac_address": "cd:9e:17:64:1b:42",
    "question": "Help me",
    "time": "2016-03-16 16:22:08",
    "is_answered": false
  }
]

  [
      {
        "id": "34",
        "mac_address": "cd:9e:17:64:1b:42",
        "question": "Help me",
        "time": "2016-03-16 16:25:29",
        "is_answered": true
      }
    ]

有一些我的客户反馈列表:

<div class="parent" ng-repeat="customer in customers">
  <h2>{{customer.id}}</h2>
  <p>{{customer.question}}</p>
  <h4 ng-show="{{customer.is_answered}}">Answered</h4>
  <h4 ng-show="!{{customer.is_answered}}">Not Answered</h4>
  <button ng-show="!{{customer.is_answered}}" ng-click="showModal()">Reply</button>
</div>

当我点击回复按钮时,会出现带有一些输入的模态以回复我的客户投诉。

<div id="modal">
<textarea placeholder=""response></textarea>
<button ng-click="submit()">Reply</button>
</div>

我想根据反馈 ID 进行更新,再说一遍,最佳做法是什么?

最佳答案

您可以通过 showModal 调用传递客户对象。

<div class="parent" ng-repeat="customer in customers">
  <h2>{{customer.id}}</h2>
  ...
  <button ng-show="!{{customer.is_answered}}" ng-click="showModal(customer)">Reply</button>
</div>

在您的 Controller 中,保存传入的客户。模式关闭后,更新该客户的 is_answered 属性。

 $scope.showModal = function (customer) {
   var selected_customer = customer;

   var modalInstance = $uibModal.open({
     templateUrl: 'myModalContent.html',
     controller: 'ModalInstanceCtrl',
     customer: customer
   });

   modalInstance.result.then(function () {
    selected_customer.is_answered = true;
   }
};

关于javascript - AngularJS:使用特定键以模式更新 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380629/

相关文章:

javascript - Html 对象标签 : access element in an html page

javascript - 在 JavaScript 中确定日期是否为今天的最佳方法是什么?

javascript - 获取当前单击的复选框的子输入标记

javascript - Angular 滤波器在移动设备上的性能限制

php - 如何使用 PHP 在 MySQL 表中插入数组?

javascript - 当传递的键是变量时从 json 获取值

python - 无法使 GAE Channel API 在本地计算机上工作

javascript - 重用具有不同参数的 Angular View 和 Controller

javascript - 使用 reactstrap 输入模块的 React ref 没有 Value Prop

javascript - Kendo UI 网格 - 按降序排序,然后升序排序