Angular JS没有更新嵌套对象的视图

时间:2015-09-15 13:07:57

标签: javascript ruby-on-rails angularjs

我正在使用优秀的AngularJS Rails Resources和一个对象 - 它依次具有深层嵌套对象 - 当我更新它的一些属性时,更新的属性在我重新加载页面之前不会显示在模板上。

让我们从头开始:这是我的对象:

    var invoice = Invoice.get($stateParams.invoiceId).then(function (result) {
      $scope.invoice = result;
    });

以下是我打开模态来编辑值的方法:

$scope.openEdit = function (edit) {
  $scope.editModal = $modal.open({
    templateUrl: 'editModalContent.html',
    controller: 'InvoiceShowController',
    size: 'lg'
  });
  $scope.editModal.result.then(function(select) {
  });
};

$scope.cancel = function () {
  $scope.$close();
};

$scope.ok = function () {
  $scope.invoice.update().then(function (result) {
    $scope.cancel();
    console.log(result);
  });
};

在我看来,我有以下内容:

...
<li>{{invoice.trading_details.trading_name}}</li>
<li>{{invoice.trading_details.trading_address_1}}</li>
...

在模态体中,我有以下内容:

    ...
    <div class="form-group">
      <label for="exampleInputEmail1">Trading Name</label>
      <input ng-model="invoice.trading_details.trading_name" type="text" class="form-control" id="exampleInputEmail1">
    </div>
    <div class="form-group">
      <label for="exampleInputEmail1">Trading Address Line 2</label>
      <input ng-model="invoice.trading_details.trading_address_1" type="text" class="form-control" id="exampleInputEmail1">
    </div>
    ...

因此,当我在模态中编辑属性并控制对象时,更改就在那里。当我保存并获得结果时,更改就会出现,但无论出于何种原因,视图都不会更新。

有什么想法吗?

编辑:我的整个controller

1 个答案:

答案 0 :(得分:2)

您似乎错过了resolve设置。它将数据传递给您的模态。

$scope.openEdit = function (edit) {
   $scope.editModal = $modal.open({
      templateUrl: 'editModalContent.html',
      controller: 'InvoiceShowController',
      size: 'lg',
      //notice a function is returning the data
      resolve: {
         invoice: function(){
             return  $scope.invoice;
         }
      }
  });
 };

修改的 链接到Plunker:http://plnkr.co/edit/IJvdBJrJngsNYaG39Gfh?p=preview

注意解析如何创建传递给editCtrl的实例 invoice

<强>更新 你也可以

   $scope.editModal = $modal.open({
      templateUrl: 'editModalContent.html',
      controller: 'InvoiceShowController',
      size: 'lg',
      //notice a function is returning the data
      resolve: {
         invoice: function(){
             return  Invoice.get($stateParams.invoiceId);
         }
      }
  });

...因为resolve可以处理承诺。