使用AngularJS和laravel更新多值并显示

时间:2017-12-08 07:24:22

标签: javascript angularjs laravel

[{"user_id":78936,"social_id":null,"type":2,"name":"Get Raj","first_name":"Get","last_name":"Raj","email":"test1@yopmail.com"},

{"user_id":78937,"social_id":null,"type":2,"name":"James thomas","first_name":"James","last_name":"thomas","email":"jamesthoms@yopmail.com"}] 

<div ng-repeat="user in profiles">
    <div class="col-sm-5 col-ie-5">
    <input
      type="text"
      name="entry_{{user.user_id}}"
      ng-model="update.entry"
      ng-change="save(user, update)"
      >
    </div>

    <div class="col-sm-5 col-ie-5">
    <input
      type="text"
      name="info_{{user.user_id}}"
      ng-model="update.info"
      ng-change="save(user, update)"
      >
    </div>

    <div class="col-sm-5 col-ie-5">
    <input
      type="text"
      name="textual_{{user.user_id}}"
      ng-model="update.textual"
      ng-change="save(user, update)"
      >
    </div>          
</div>


function makeOrderItemUrl(user_id, order_id){
       return 'v1/update/' + user_id + '/order_id/' + order_id;
    }

$scope.save = function (person, update) {


     var person = person;
     var order = null,
     matchCount = 0;

        for (var i = 0; i < $scope.order.items.length; i++) {
                if ($scope.order.items[i].user_id == person.user_id) {
                  order = $scope.order.items[i];
                  matchCount++;
                }
              }

      if(order){
          $http.post(makeOrderItemUrl(person.user_id, order.order_id), update)
          .success(function(res){
            $scope.update = res.data;
          }).error(function () {
            notify.message('There was an issue saving your changes, please try again later.');
          });  
      }

    };

由于我们有两个用户,所以我们将获得两次输入字段,即ng-model =“update.entry”两次,然后每当我输入第一个文本框时,它在第二个文本框中反映出相同的值,因为他们使用的模型如何设置不同的模型和值,分别保存到数据库和显示中。

Laravel 

$orderItem =  OrderItem::firstOrNew([
                    'user_id' => $user_id,
                    'order_id' => $order_id
                    ]); 

if($orderItem->exists){
            $orderItem->entry = Input::get('entry');    
            $orderItem->save();
        }

//这个工作正常,但很难显示。

1 个答案:

答案 0 :(得分:0)

使update更深层的对象,其中用户ID被用作第一级密钥

<input
      type="text"
      name="entry_{{user.user_id}}"
      ng-model="update[user.user_id].entry"
      ng-change="save(user, update[user.user_id])"
      >

然后$scope.update看起来像:

{
  id1: {
    textual: '...',
    entry: '...',
    info: '...'
  },
  id2: {
    textual: '...',
    entry: '...',
    info: '...'
  }
}