我们如何链接$ uibModal对话框?

时间:2018-03-24 19:05:55

标签: angularjs ui.bootstrap

我是AngularJS的初学者。现在我想使用ui.bootstrap $ uibModal服务创建对话框链。我想从.txt文件接收数据到对话框。这意味着,我最初不知道链中对话框的数量,它将取决于.txt数据。现在我试着用循环来做到这一点,但它很愚蠢,然后我就不能拥有像以前那样的功能"," next",ok并取消。这样做有什么明智的解决方案吗?提前谢谢您的回答。



var app = angular.module('myApp', ['ui.bootstrap']);

angular.module('myApp').config(['$uibModalProvider', function($uibModalProvider) {
  $uibModalProvider.options.windowClass = 'show';
  $uibModalProvider.options.backdropClass = 'show';
}]);



app.factory('modalDialog', function($uibModal) {
    var arr_items = [];

    var showModalDialog =function(items) {
      return $uibModal.open({
         templateUrl:  'test.html', //just the dialog body for the info
         animation: true,
          size: 'sm',
          ariaLabelledBy: 'modal-title',
          ariaDescribedBy: 'modal-body',
          controller: 'dialogCtrl',
          controllerAs: "$ctrl",
          resolve: { items: function () { console.log("items before travelling" + JSON.stringify(items));
          return items; } }
        });
      
    }

     return {showModalDialog: showModalDialog};


});


app.controller('TestCtrl', function($scope, $http, modalDialog){
  $scope.cancelled = false;
  $scope.test = function(){
    $http.get('test.txt')
        .then(function(response) {
           $scope.items=response.data;
           for (var i=0; i<$scope.items.length; i++) {
            console.log($scope.cancelled); 
            if ($scope.cancelled) return ;
           var showModalDialog = function() {
              console.log("here");
              modalDialog.items = $scope.items[i];
              modalDialog.showModalDialog($scope.items[i]);
            };
            showModalDialog();
          };
        });
  };
});


app.controller('dialogCtrl', function ($scope, $uibModalInstance, items) {
  var $ctrl =this;
  console.log("here are my items" + items.request + " " + JSON.stringify(items));

  $scope.items = items;

  $ctrl.ok = function () {
    $uibModalInstance.close($scope.items);
  };

    $ctrl.cancel = function () {
      console.log("cancel");
      $scope.cancelled = true;
      return $uibModalInstance .dismiss('cancel');
  };
});
&#13;
<!doctype html>
<html ng-app="myApp">
  <head>
  	<meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.js"></script>
    <script src="ui-bootstrap-tpls-2.5.0.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
<div ng-controller="TestCtrl">
    <button class="btn btn-primary" ng-click="test()">Test</button>
</div>

	<script type="text/ng-template" id="test.html">
		<div><div class="modal-header">
  <h1 class="modal-title">Test Modal</h1>
</div>
<div class="modal-body">
  Test Data:
<div>{{items}}</div>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button>
    <button class="btn btn-primary" type="button" ng-click="$ctrl.cancel()">Cancel</button>
</div>
</div>

	</script>



    <script src="script.js"></script>


  </body>
</html>
&#13;
&#13;
&#13;

这是我的test.txt文件:

[{
        "request": "new user",
        "dialogs": [
            ["input", "checkbox"],
            ["input", "radiobutton"],
            ["input", "input"]
        ]
    },
    {
        "request": "new project",
        "dialogs": [
            ["input", "input"],
            ["radiobutton", "radiobutton"],
            ["input", "input"]
        ]
    },
    {
        "request": "delete project",
        "dialogs": [
            ["checkbox", "checkbox"],
            ["input", "radiobutton"],
            ["input", "input"]
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

Process class

循环的主要逻辑在于

function initiateModal(itemList,index){
  modalDialog.showModalDialog(itemList[index]).result
    .then(function(resp){
              if(resp === 'cancel'){ // if user cancel the process in between
                $scope.userSeletion.length = 0;
                return ;
              }else if(itemList.length === (index+1)){ // for pushing the last modal data into array
                $scope.userSeletion.push(resp);
                return ;
              }else{
                $scope.userSeletion.push(resp);
                index++;
                initiateModal (itemList,index);
              }
       })
} 

等待早期模态的promise打开下一个模态。

目前,我正在传递userInput作为数据,我可以根据用户表单输入填写