2017-04-07 83 views
0

我想知道如果我可以簡化UI-bootstrap的模式,像bootstrap中的原始模式,因爲角度是一堆代碼,我真的與ui-bootstrap有一個噩夢用於角度的模態。看起來代碼只是用於定製模式等,但你需要添加功能打開等。最簡單的角度用戶界面引導模式

如果我需要其他模板與其他模板?或者如果我需要很多模態?

這是我從ui-bootstrap參考幫助獲得的代碼。我試圖刪除所有不知道的代碼,但仍然太複雜。

VIEW

<div ng-controller="ModalDemoCtrl as $ctrl" class="modal-demo"> 
    <script type="text/ng-template" id="myModalContent.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title" id="modal-title">I'm a modal!</h3> 
     </div> 
     <div class="modal-body" id="modal-body"> 
      <ul> 
       <li ng-repeat="item in $ctrl.items"> 
        <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a> 
       </li> 
      </ul> 
      Selected: <b>{{ $ctrl.selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button> 
      <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button> 
     </div> 
    </script> 


    <button type="button" class="btn btn-default" ng-click="$ctrl.open('lg')">Large modal</button> 
</div> 

APP

// MODAL CONTROLLERS 
myApp.controller('ModalDemoCtrl', function ($uibModal, $log, $document) { 
    var $ctrl = this; 
    $ctrl.items = ['item1', 'item2', 'item3']; 

    $ctrl.animationsEnabled = true; 

    $ctrl.open = function (size, parentSelector) { 
     var parentElem = parentSelector ? 
     angular.element($document[0].querySelector('.modal-demo ' + parentSelector)) : undefined; 
     var modalInstance = $uibModal.open({ 
      animation: $ctrl.animationsEnabled, 
      ariaLabelledBy: 'modal-title', 
      ariaDescribedBy: 'modal-body', 
      templateUrl: 'myModalContent.html', 
      controller: 'ModalInstanceCtrl', 
      controllerAs: '$ctrl', 
      size: size, 
      appendTo: parentElem, 
      resolve: { 
       items: function() { 
        return $ctrl.items; 
       } 
      } 
     }); 

     modalInstance.result.then(function (selectedItem) { 
      $ctrl.selected = selectedItem; 
     }, function() { 
      $log.info('Modal dismissed at: ' + new Date()); 
     }); 
    }; 

    $ctrl.openComponentModal = function() { 
     var modalInstance = $uibModal.open({ 
      animation: $ctrl.animationsEnabled, 
      component: 'modalComponent', 
      resolve: { 
       items: function() { 
        return $ctrl.items; 
       } 
      } 
     }); 

     modalInstance.result.then(function (selectedItem) { 
      $ctrl.selected = selectedItem; 
     }, function() { 
      $log.info('modal-component dismissed at: ' + new Date()); 
     }); 
    }; 

    $ctrl.openMultipleModals = function() { 
     $uibModal.open({ 
      animation: $ctrl.animationsEnabled, 
      ariaLabelledBy: 'modal-title-bottom', 
      ariaDescribedBy: 'modal-body-bottom', 
      templateUrl: 'stackedModal.html', 
      size: 'sm', 
      controller: function($scope) { 
       $scope.name = 'bottom'; 
      } 
     }); 

     $uibModal.open({ 
      animation: $ctrl.animationsEnabled, 
      ariaLabelledBy: 'modal-title-top', 
      ariaDescribedBy: 'modal-body-top', 
      templateUrl: 'stackedModal.html', 
      size: 'sm', 
      controller: function($scope) { 
       $scope.name = 'top'; 
      } 
     }); 
    }; 

    $ctrl.toggleAnimation = function() { 
     $ctrl.animationsEnabled = !$ctrl.animationsEnabled; 
    }; 
}); 

// Please note that $uibModalInstance represents a modal window (instance) dependency. 
// It is not the same as the $uibModal service used above. 

myApp.controller('ModalInstanceCtrl', function ($uibModalInstance, items) { 
    var $ctrl = this; 
    $ctrl.items = items; 
    $ctrl.selected = { 
     item: $ctrl.items[0] 
    }; 

    $ctrl.ok = function() { 
     $uibModalInstance.close($ctrl.selected.item); 
    }; 

    $ctrl.cancel = function() { 
     $uibModalInstance.dismiss('cancel'); 
    }; 
}); 

// Please note that the close and dismiss bindings are from $uibModalInstance. 

myApp.component('modalComponent', { 
    templateUrl: 'myModalContent.html', 
    bindings: { 
     resolve: '<', 
     close: '&', 
     dismiss: '&' 
    }, 
    controller: function() { 
     var $ctrl = this; 

     $ctrl.$onInit = function() { 
      $ctrl.items = $ctrl.resolve.items; 
      $ctrl.selected = { 
       item: $ctrl.items[0] 
      }; 
     }; 

     $ctrl.ok = function() { 
      $ctrl.close({$value: $ctrl.selected.item}); 
     }; 

     $ctrl.cancel = function() { 
      $ctrl.dismiss({$value: 'cancel'}); 
     }; 
    } 
}); 

正如你所看到的,是一串代碼。唯一想我要的是有舊的時尚道路的選擇,如:

與形式上ID按鈕

模式與內容

顯然,這是不可能的ui-bootstrap,任何幫助將被折扣

+0

如果你在應用程序中創建了一個包含所有'$ uibModal.open()'代碼的服務的許多不同的模塊,可以減少很多代碼複製 – charlietfl

+0

據我所知,你需要一個根據與該ID關聯的ID和內容是否正確? –

+0

事情是,我實現了,它的工作原理,但只是一個模態模板,我只能有一個內容安排,我不明白,但可以有不同的設計呢?或者需要複製我的應用程序和其他模式的整個代碼?我注意到的其他事情是我不能在控制器外部使用模態。我已經有一個名爲homeController的控制器,裏面有這個模式的控制器。對我來說,與原始的引導方式相比,是一團糟。 –

回答

1

我必須同意你對這一個,我發現文檔對我來說有點冗長。如您所見,我的templateURL在包含模態內容的相同目錄中引用了單獨的.html文件。

這裏是我的解決方案是什麼樣子:

在我的控制器:

myApp.controller('ModalController', function ($log, $uibModal, $scope) { 
    $log.debug('ModalController'); 

    this.createModal = function() { 
    let modal = $uibModal.open({ 
     backdrop: 'static', 
     keyboard: true, 
     backdropClick: false, 
     template: require('./modal-content.html'), 
     scope: $scope 
    }); 

    $scope.modalInstance = modal; 
    return modal.result; 
    }; 

    this.triggerModal = function() { 
    this.createModal() 
    .then((data) => { 
     this.handleSuccess(data); 
    }) 
    .then(null, (reason) => { 
     this.handleDismiss(reason); 
    }); 
    }; 

    this.yes = function() { 
    $scope.modalInstance.close('Yes Button Clicked'); 
    }; 

    this.no = function() { 
    $scope.modalInstance.dismiss('No Button Clicked'); 
    }; 

    this.handleSuccess = function(data) { 
    $log.info('Modal closed: ' + data); 
    }; 

    this.handleDismiss = function(reason) { 
    $log.info('Modal dismissed: ' + reason); 
    }; 
} 

這裏是 '模式,content.html' 的內容是這樣的:

<div class="modal-header"> 
    <h3 class="modal-title">I'm a modal!</h3> 
</div> 
<div class="modal-body"> 
    <ul> 
    <li>item 1</li> 
    <li>item 2</li> 
    <li>item 3</li> 
    </ul> 
</div> 
<div class="modal-footer"> 
    <button class="btn btn-primary" ng-click="landingModalCtrl.yes()">yes</button> 
    <button class="btn btn-warning" ng-click="landingModalCtrl.no()">no</button> 
</div> 

。希望幫助!

+0

謝謝!我會試試這個解決方案。你是對的,我不太瞭解文件,但ui boostrap文件很難遵循,特別是從設計背景的人 –

+0

Yah沒問題,我希望有幫助。我的Angular實現與我們在這裏使用的語法略有不同(我實際上在我的應用中使用組件化結構),但是我修改它以類似於ui-bootstrap文檔中的示例。讓我知道,如果我能進一步幫助你。 –