2013-12-15 21 views
10

私のデータを編集するためにモーダルを使いたいです。データをモーダルインスタンスに渡します。私がokをクリックすると、$ scope.selectedで編集したデータをコントローラに渡します。

元の$有効範囲を更新したいと思います。何とか$ scopeは更新されません。私は間違って何をしていますか?

var ModalDemoCtrl = function ($scope, $modal, $log) { 

    $scope.data = { name: '', serial: '' } 

    $scope.edit = function (theIndex) { 

    var modalInstance = $modal.open({ 
     templateUrl: 'myModalContent.html', 
     controller: ModalInstanceCtrl, 
     resolve: { 
     items: function() { 
      return $scope.data[theIndex]; 
     } 
     } 
    }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 

     // this is where the data gets updated, but doesn't do it 
     $scope.data.name = $scope.selected.name; 
     $scope.data.serial = $scope.selected.serial; 

    }); 
    }; 
}; 

モーダルコントローラー:

var ModalInstanceCtrl = function ($scope, $modalInstance, items) { 

    $scope.items = items; 
    $scope.selected = { 
    name: $scope.items.name, 
    serial: $scope.items.serial 
    }; 

    $scope.ok = function() { 
    $modalInstance.close($scope.selected); 
    }; 

    $scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
    }; 
}; 

モーダル:

<div class="modal-header"> 
    <h3>{{ name }}</h3> 
</div> 
<div class="modal-body"> 
    <input type="text" value="{{ serial }}"> 
    <input type="text" value="{{ name }}"> 
</div> 
<div class="modal-footer"> 
    <button class="btn btn-primary" ng-click="ok()">OK</button> 
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 
</div> 
+0

私はちょっとタイプミスですが、問題は解決しません: – Tino

答えて

14

あなたはモーダルのためのテンプレートが含まれていませんでしたので、これは推測のビットです。あなたのコードは、テンプレート内でng-repeatを使用している角度-uiモーダルのサンプルコードにかなり近いです。同じことをしている場合は、ng-repeatが親から継承する子スコープを作成することに注意してください。

$scope.ok = function() { 
    $modalInstance.close($scope.selected); 
}; 

ではなく、あなたのテンプレートでこれを行うようになっています:

<li ng-repeat="item in items"> 
    <a ng-click="selected.item = item">{{ item }}</a> 
</li> 

あなたはこのような何かやっている可能性があります場合は

<li ng-repeat="item in items"> 
    <a ng-click="selected = item">{{ item }}</a> 
</li> 

をこのスニペットから判断

あなたのケースでは、子スコープにselectedを割り当てています。これは影響を受けません親スコープのselectedプロパティその後、$scope.selected.nameにアクセスしようとすると、空になります。 一般に、モデルにオブジェクトを使用し、そのモデルにプロパティを設定し、新しい値を直接割り当てる必要はありません。

This part of the documentationは、範囲の問題をより詳細に説明します。

編集:

あなたはまた、まったくのモデルに、あなたの入力を結合していないので、あなたが入力したデータがどこにも保存されることはありません。あなたは、例えば、それを行うためにng-modelを使用する必要があります。:

<input type="text" ng-model="editable.serial" /> 
<input type="text" ng-model="editable.name" /> 

参照this plunkr取り組ん例えば。

+0

thx私はちょうど "名前"と "シリアル"を更新するために2つの入力を使用しています。 No Ng-Repeat – Tino

+0

私はモーダルを追加しました – Tino

+0

@Tino私の答えは –

関連する問題