2015-10-18 2 views
0

Я использую angularstrap для компонентов пользовательского интерфейса на моем сайте SPA. Я модальная ведьму я создаю динамически, как этот код:Как сменить шаблон urlbootstrap modal динамически?

$scope.modal = $modal(
      { controller: "testCtrl", 
       show : false, 
       placement : top 
      }); 

и у меня есть пара кнопки, такие как это:

<button type="button" ng-click="setTemplate(1)" ></button> 
<button type="button" ng-click="setTemplate(2)" ></button> 

и в моем контроллере у меня есть эта функция:

$scope.setTemplate = function (val){ 
     if (val == 1){ 
     $scope.modal.templateUrl ="path/to/firsttemp.html" ; 
     }else if (val== 2){ 
     $scope.modal.templateUrl ="path/to/secondtemp.html" ; 
     } 
} 

теперь, когда я нажимаю на каждую кнопку, мой модальный пуст.

ответ

1

Вы можете изменить, как это,

в app.js,

$scope.open = function (val) { 
    if (val == 1){ 
      $scope.modal.templateUrl ="path/to/firsttemp.html" ; 
     }else if (val== 2){ 
      $scope.modal.templateUrl ="path/to/secondtemp.html" ; 
     } 
    var modalInstance = $uibModal.open({ 
     animation: $scope.animationsEnabled, 
     templateUrl: $scope.modal.templateUrl , 
     controller: 'ModalInstanceCtrl', 
     resolve: { 
     items: function() { 
      return $scope.items; 
     } 
     } 
    }); 

в HTML,

<div ng-controller="ModalDemoCtrl"> 
    <script type="text/ng-template" id="path/to/firsttemp.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title">This is template 1</h3> 
     </div> 
     <div class="modal-body"> 
      <ul> 
       <li ng-repeat="item in items"> 
        <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
       </li> 
      </ul> 
      Selected: <b>{{ selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> 
      <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> 
     </div> 
    </script> 
    <script type="text/ng-template" id="path/to/secondtemp.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title">This is template 2</h3> 
     </div> 
    </script> 

    <button type="button" class="btn btn-default" ng-click="open('1')">template 1</button> 
    <button type="button" class="btn btn-default" ng-click="open('2')">template 2</button> 
</div> 

link plunker за то же самое.

Надеюсь, это поможет. .)