2015-01-29 2 views
5

Я работаю с директивой angularjs для всплывающего окна. Когда я использую директиву один раз, он отлично работает, но когда я использую i более одного раза, он не работает. Я не понимаю, что делаю неправильно. Вот мой код.modalDialog с директивой в угловом выпуске

HTML

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></script> 
<meta charset=utf-8 /> 
<title>JS Bin</title> 
</head> 
<body ng-app='ModalDemo'> 
    <div ng-controller='MyCtrl'> 
    <button ng-click='toggleModal()'>Open First Dialog</button> 
    <button ng-click='toggleModal1()'>Open Second Dialog</button> 
    <modal-dialog info='modalShown' show='modalShown' width='400px' height='60%'> 
     <p>Modal Content Goes here<p> 
    </modal-dialog> 
     <modal-dialog show='modalShown1' info='modalShown1' width='400px' height='60%'> 
     <p>2<p> 
    </modal-dialog> 
    </div> 
</body> 
</html> 

JS

app = angular.module('ModalDemo', []); 
    app.directive('modalDialog', function() { 
     return { 
     restrict: 'E', 
     scope: { 
      show: '=info' 
     }, 
     replace: true, // Replace with the template below 
     transclude: true, // we want to insert custom content inside the directive 
     link: function(scope, element, attrs) { 
      scope.dialogStyle = {}; 
      if (attrs.width) 
      scope.dialogStyle.width = attrs.width; 
      if (attrs.height) 
      scope.dialogStyle.height = attrs.height; 
      scope.hideModal = function() { 
      scope.show = false; 
      }; 
     }, 
     template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>" 
     }; 
    }); 

    app.controller('MyCtrl', ['$scope', function($scope) { 
     $scope.modalShown = false; 
     $scope.toggleModal = function() { 
     $scope.modalShown = !$scope.modalShown; 
     }; 
    $scope.modalShown1 = false; 
     $scope.toggleModal1 = function() { 
     $scope.modalShown1 = !$scope.modalShown1; 
     }; 
    }]); 

Вот пример jsbin

Пожалуйста, помогите.

ответ

3

Я думаю, что это просто так:

<p>Modal Content Goes here<p> 

и

<p>2<p> 

Не закрывать теги!

<p>Modal Content Goes here</p> 

и

<p>2</p> 

Если это исправить: Working jsbin.

1

Были пара проблем с подходом. Сначала были закрыты скобки, и ваша шкура не работала.

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

Пожалуйста, смотрите здесь:

http://jsbin.com/yaqilaliti/2/

Смежные вопросы