2016-04-03 12 views
2

jsonを特定のキーで更新するにはどうすればよいですか? 私の場合、フィードバックを「応答しない」から「応答しました」に更新したいと考えています。 AngularJS:特定のキーでモーダルでJSONを更新

<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> 

私は返信ボタンをクリックし

が、その後、応答に私の顧客の苦情をいくつか入力してモーダルを表示されます。

[ 
    { 
    "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 id="modal"> 
<textarea placeholder=""response></textarea> 
<button ng-click="submit()">Reply</button> 
</div> 

私はフィードバックIDに基づいて更新したいと思います。また、どのようにそれを行うのがベストプラクティスですか?

+0

君たちに感謝私たちはあなたのコントローラのコードを見ることができますか? – JordanHendrix

+0

個人的に私はlodash findIndexを使ってidでアイテムを取り出し、更新を実行します - https://lodash.com/docs#findIndex – dmoo

+0

私は最近このリンクを見つけました:[link](http://stackoverflow.com/questions/ 30917576/angularjs-open-modal-with-specific-data-from-array)、類似のトピック – artemist

答えて

1

showModalコールでcustomerオブジェクトを渡すことができます。お使いのコントローラ内部

<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> 

、これは顧客に渡されたセーブ。モーダルが閉じられると、その顧客の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; 
    } 
}; 
+0

私はまだ混乱しています..ok、簡略化した私の質問は、私のモーダルで質問内容を表示する方法です。実際には、特定のIDを持つ$ mdDialogを使用しています。 – artemist

0

Pass input value to $mdDialogに基づいて私の解決策が見つかりました。私の見解では

vm.showModal = function(e, message) { 

     $mdDialog.show({ 
     clickOutsideToClose:true, 
     // locals:{ name: 'Bob'}, 
     controller: function ($mdDialog) { 
      var vm = this; 
      vm.message = {}; 
      vm.message = message; 

      $scope.hide = function() { 
      $mdDialog.hide(); 
      }; 
      $scope.cancel = function() { 
      $mdDialog.cancel(); 
      }; 
     }, 
     controllerAs: 'modal', 
     templateUrl: 'feedbackForm.html', 
     parent: angular.element(document.body), 
     targetEvent: e, 
     }); 
    }; 

<h5 style="">{{modal.message.id}}</h5> 

関連する問題