如何将angularJS ui.bootstrap对话框(带有动态输入)中更改的输入数据传递回结果?
我有一个简单的CRUD前端和AngularJS,我需要打开一个对话框,其中填充了范围数据的输入。数据本身是完全不同的,需要表单是动态的。保存时,我需要更新根作用域的数据结构 当前,控制器范围的项以某种方式绑定回根范围的数据结构。我如何解决这个问题?我需要将这些更新的值传递给对话框results promise,然后该对话框将写入根范围的数据结构如何将angularJS ui.bootstrap对话框(带有动态输入)中更改的输入数据传递回结果?,angularjs,ui.bootstrap,Angularjs,Ui.bootstrap,我有一个简单的CRUD前端和AngularJS,我需要打开一个对话框,其中填充了范围数据的输入。数据本身是完全不同的,需要表单是动态的。保存时,我需要更新根作用域的数据结构 当前,控制器范围的项以某种方式绑定回根范围的数据结构。我如何解决这个问题?我需要将这些更新的值传递给对话框results promise,然后该对话框将写入根范围的数据结构 {{name}}: OK 示例JS: angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
{{name}}:
OK
示例JS:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {
$scope.data = [{name: 'A', url: 'example.com', interval: 5}, {name: 'B', node: 'A'},{name: 'C', different:"component"}];
$scope.open = function (index) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
item: function () {
return {index, data: $scope.data[index]};
}
}
});
modalInstance.result.then(function (item) {
console.log('result:', item);
$scope.data[item.index] = item.data
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, item) {
$scope.item = item;
console.log("dialog scope:", $scope);
$scope.save = function () {
console.log("save item: ", $scope.item);
$modalInstance.close($scope.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
HTML示例:
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.0/angular.min.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<div>Index: {{item.index}}</div>
<div ng-repeat="(name, value) in item.data">
{{name}}: <input name="{{name}}" ng-value="value" ng-model="item.data[name]"/>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="save(item)">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<div ng-repeat="(key, obj) in data">
<button class="btn btn-default" ng-click="open(key)">Edit {{obj.name}}</button>
</div>
<div ng-repeat="(key, obj) in data">
<div>
{{key}}
</div>
<label ng-repeat="(name, val) in obj">- {{name}}: {{val}} -</label>
</div>
</div>
</body>
</html>
我是模态的!
索引:{{item.Index}
{{name}}:
好啊
取消
编辑{{obj.name}
{{key}}
-{{name}}:{{val}-