如何解决如何将已更改的输入数据从angularJS ui.bootstrap对话框带有动态输入传递回结果承诺?
我有一个使用AngularJS的简单CRUD前端,我需要打开一个对话框,其中装有范围数据的输入。数据本身是不同的,并且需要动态的形式。保存时,我需要更新根范围数据结构。
当前,控制器范围内的项以某种方式绑定回了根范围内的数据结构。我该如何解决?我需要将这些更新的值传递给对话框结果承诺,然后将其写入根范围的数据结构。
{{name}}: <input name="{{name}}" ng-value="value" ng-model="???"/>
...
<button class="btn btn-primary" ng-click="save(???)">OK</button>
示例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',$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>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)