如何将已更改的输入数据从angularJS ui.bootstrap对话框带有动态输入传递回结果承诺?

问题描述

我有一个使用AngularJS的简单CRUD前端,我需要打开一个对话框,其中装有范围数据的输入。数据本身是不同的,并且需要动态的形式。保存时,我需要更新根范围数据结构。

当前,控制器范围内的项以某种方式绑定回了根范围内的数据结构。我该如何解决?我需要将这些更新的值传递给对话框结果承诺,然后将其写入根范围的数据结构。

see fiddle

{{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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...