【问题标题】:Angularjs popup/modal contentAngularjs弹出/模态内容
【发布时间】:2018-04-29 17:19:52
【问题描述】:

我正处于 AngularJS 边做边学的阶段,对此我无法弄清楚也无法找到答案。

我在做什么:

在 ng-click 上,我在控制器中调用一个函数,即 $http-loads html-content(一些 div 和一个表单),我将其“粘贴”到模板中 div 内的 ng-bind-html。

这很好 - 但我的问题是,我无法在加载的数据中为 ng-models 或 {{someText}} 设置值。我在 div 内的元素上得到“未定义”(具有随机内容的模态 div,因此不是我模板的静态部分)。

我没有使用 Bootstrap 或类似的东西。

我可以做些什么来使数据成为我范围的一部分 - 或者以另一种方式实现我想要的(具有随机内容的模态 div)?

如果对您有帮助,我可以发布一些代码。当你回答时请记住,我是一个完全新手! :-)

-

我的指令:

workApp.directive('modalDialog', function() {  
    return {  
        restrict: 'E',  
        scope: {  
            show: '='  
        },  
        transclude: true,  
        link: function(scope, element, attrs) {  
            scope.dialogStyle = {};  
            if (attrs.boxWidth) scope.dialogStyle.width = attrs.boxWidth;  
            if (attrs.boxHeight) scope.dialogStyle.height = attrs.boxHeight;  
            scope.hideModal = function() {  
                scope.show = false;  
            };  
        },  
        templateUrl: 'app/tpl/modal.html'  
    };  
}); 

州:

.state('main', {  
    abstract: true,
    templateUrl: tplMain  
})  

    // PROJECTS

    .state('main.projects', {  
        url: '/projects',
        templateUrl: 'app/views/projects/project-list.html',  
        controller: 'projectListCtrl'  
    })  

    .state('main.projectdetails', {  
        url: '/projects/:projectId/details',  
        templateUrl: 'app/views/projects/project-details.html',  
        controller: 'projectDetailsCtrl'  
    })  

HTML(嵌套视图):

<!-- main -->
<div ui-view>   
    <!-- main.projects -->  
    <div ui-view>
        <a ng-click="newProject()">New project</a>
    </div>
</div>
<modal-dialog>{{message}}</modal-dialog>

控制器:

workApp.controller('projectListCtrl', function($scope, $rootScope, $http) {
    $scope.newProject = function() {
        $scope.message = '<div>Some HTML here...</div>'; // Loaded from $http.get
        $scope.modalOpen = true;
    }
});

我的两个问题中的第一个是,当我调用 newProject() 时模式没有显示。我认为这是因为状态/嵌套视图(我的模式对话框在另一个视图中)?如果我将模式对话框复制到我的 main.projects 状态,它就可以工作。

第二个是{{message}}不能包含绑定,所以我无法绑定fx。 $scope.modal.title 到 HTML 中的 {{modal.title}}。

更新:

我找到了一个动态包含 html 文件的工作示例:

<div id="modal" ng-class="{ open: modal.data.visible }" ng-include="'app/views/' + modal.data.include"></div>

在控制器中:

$scope.modal.data = {  
    include: 'projects/project-data.html',  
    visible: true,  
    title: 'New project',  
    subtitle: 'Enter project data',  
    projectName: 'My first project',  
    projectCompany: 'The Project Company'  
}

这似乎工作得很好 - 但与指令相比,这是不好的做法(我仍然无法开始工作)。

【问题讨论】:

  • 你可能想要一个可以$compile它的指令
  • 定义模型的控制器是否连接到调用它们的 html 元素?前任。
  • 我找到了一个更简单的版本,它将 $scope 中的消息绑定到模态,我在代码中进行了修改,看看是否有帮助。

标签: angularjs modal-dialog


【解决方案1】:

如果我了解您的用例,您只是想在 html 和模态框之间传递数据。这是一个Plunker,我发现你可以查看。我做过类似的事情,指令是我找到的最佳解决方案。

HTML

<!DOCTYPE html>
<html ng-app="app">

 <head>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" data-semver="3.3.2" data-require="bootstrap@*" />
  <script src="http://code.jquery.com/jquery-2.1.4.min.js" data-semver="2.1.4" data-require="jquery@*"></script>
  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js" data-semver="3.3.2" data-require="bootstrap@*"></script>
  <script data-require="angular.js@1.3.15" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
 </head>

  <body ng-controller="indexCtrl">
  <h4 style="margin-left: 10px;margin-top: 20px;">Display a modal dialog with AngularJS</h4>
  <hr />
  <button ng-click="toggleModal()" class='btn btn-warning btn-sm' style="margin: 5px 0px 10px 10px;width: 150px;">Open Modal</button><br />

  <!-- Angular JS Directive Modal -->
  <modal-dialog box-width="400px" box-height="150px" show="modalShown">
    <div class="row">
      <div class="col-md-12">
        <h3>Header</h3>
        <hr style="border-top:1px solid darkblue"/>
      </div>
    </div>

    <div class="row">
      <div class="col-md-12">
        <!-- This is an important message -->
        {{message}}
      </div>
    </div>
  </modal-dialog>
</body>

</html>

modalDialog.html

<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>

script.js

    angular.module('app', []);

angular.module('app').controller('indexCtrl', function($scope) {
  $scope.modalShown = false;
  $scope.message= "This is an important message!"
  $scope.toggleModal = function() {
    $scope.modalShown = !$scope.modalShown;
  };
});

angular.module('app').directive('modalDialog', function() {
  return {
    restrict: 'E',
    scope: {
      show: '='
    },
    transclude: true, // Insert custom content inside the directive
    link: function(scope, element, attrs) {
      scope.dialogStyle = {};
      if (attrs.boxWidth) {
        scope.dialogStyle.width = attrs.boxWidth;
      }
      if (attrs.boxHeight) {
        scope.dialogStyle.height = attrs.boxHeight;
      }
      scope.hideModal = function() {
        scope.show = false;
      };
    },
    templateUrl: 'modalDialog.html'
  };
});

CSS

/* Styles go here */
.ng-modal-overlay {
  /* A dark translucent div that covers the whole screen */
  position:absolute;
  z-index:9999;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background-color:#808080;
  opacity: 0.8;
}

.ng-modal-dialog {
  background-color: #fff;
  box-shadow: 10px 10px #595554;
  border-radius: 4px;
  z-index:10000;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.ng-modal-dialog-content {
  padding:10px;
  text-align: left;
}

.ng-modal-close {
  position: absolute;
  top: 3px;
  right: 5px;
  padding: 5px;
  cursor: pointer;
  font-size: 120%;
  display: inline-block;
  font-weight: bold;
  font-family: 'arial', 'sans-serif';
}

【讨论】:

  • 我正在尝试实现这一点,但正如原帖中所说,我没有使用 Bootstrap,所以我遇到了一些错误。没有它可以做到吗?
  • 是的,但我会选择尝试从我提供的链接中复制 plunker 中的代码。对于 Angularjs 的初学者来说,自定义指令并不容易,但是修改一个工作示例,尽管一个更复杂的示例会对您有所帮助。除非您以前做过,否则您的用例并不是一个简单的用例..
  • 但是如何从控制器更改指令的“内容”并使绑定起作用?如果我的指令是 ,我如何从我的控制器中决定其中的内容?这可能是因为我没有看到指令的强度(或/并且知道如何使用它),但是为什么我不能通过 $http.get 加载一些内容并将其附加到 $compile 到一个 div ? /跨度>
  • 您可以删除引导代码,但在我看来,它更易于使用。看看这个修改后的代码是否能更好地说明事情而没有复杂的噪音。
  • 非常感谢您提供的最后一个示例!这几乎就是我一段时间以来一直在摆弄的东西。 2件事似乎对我不起作用。 1:如何使 {{message}} 与绑定一起使用?请记住,我不能将模式的内容更改为几乎所有内容(使用 $http 加载的主要表单)。和 2:我正在使用带有嵌套视图/控制器的 ui-router。我希望模态在我的主控制器(第一级)中,这样我就可以从我不想要的任何地方调用模态。但似乎 必须在调用它的同一控制器中?
猜你喜欢
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多