我想使用模態來編輯我的數據。我將數據傳遞給模態實例。當我單擊確定時,我將$ 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>
呵呵thx你小錯字,但並沒有解決問題:-) – Tino