javascript - modalDialog 带有 Angular 、范围问题的指令

标签 javascript angularjs angularjs-directive

我正在为一个弹出窗口使用 angularjs 指令。当我使用一次指令时它工作正常但是当我使用我不止一次时它不起作用。我不明白我做错了什么。这是我的代码。

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

请帮忙。

最佳答案

我认为是这样的:

<p>Modal Content Goes here<p>

<p>2<p>

不关闭标签!

<p>Modal Content Goes here</p>

<p>2</p>

应该修复它:Working jsbin .

关于javascript - modalDialog 带有 Angular 、范围问题的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210753/

相关文章:

javascript - 无法访问 $watch 内的范围变量

javascript - 结合 JS 客户端验证时,不会触发 JSF ajax 请求

javascript - Sip 在 AngularJs 中不工作

javascript - 编译嵌入模板

javascript - Angularjs 在指令之间共享 Controller

javascript - Chrome 扩展程序权限

Phantomjs 上的 javascriptEnabled 问题

java - 如何在AngularJS中使用JAVA创建的 session

javascript - 我想用所选项目的数据填充我的编辑表单

javascript - ng-click 图像以选中单选框