如何在 UI 引导模式中传递 HTML 标记 [英] How to pass HTML markup in UI bootstrap modal

查看:23
本文介绍了如何在 UI 引导模式中传递 HTML 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UI 引导程序来创建模态对话框.如果我使用templateUrl"并使用ng-template"来包含html模板,它就可以正常工作.但是由于我有多个模态对话框,在使用ng-template"将所有 html 模板包含在页面中后,我的页面变得越来越大.

代码如下:HTML

<script data-require="angular.js@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.min.js"><script data-require="angular-animate@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular-animate.js"><script data-require="ui-bootstrap@1.3.2" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script><script src="script.js"></script><link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/><身体><div ng-controller="myCtrl"><script type="text/ng-template" id="myContent.html"><div class="modal-header"><h3 class="modal-title">我是模态!</h3>

<div class="modal-body">模态正文内容在这里...

<div class="modal-footer"><button class="btn" type="button" ng-click="cancel()">关闭</button>

<button type="button" class="btn btn-default" ng-click="open()">显示模态</button>

</html>

Javascript:

angular.module('mydemo', ['ngAnimate', 'ui.bootstrap']);angular.module('mydemo').controller('myCtrl', function ($scope, $uibModal, $log) {$scope.open = 函数(大小){var modalInstance = $uibModal.open({templateUrl: 'myContent.html',控制器:'ModalInstanceCtrl',尺寸:尺寸});};});angular.module('mydemo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance) {$scope.cancel = 函数 () {$uibModalInstance.dismiss('cancel');};});

有什么办法可以直接使用 HTML 标记而不是使用templateUrl"?或者至少有一种方法可以将 html 模板保存在单独的文件中,而不是使用 ng-template 将其包含在页面中?这是工作示例:http://plnkr.co/edit/HqThAG79r22K2VGZSs2D?p=preview

解决方案

是的,您有几个选项可以指示模态将加载哪些内容.

  1. 正如@Wes 在他的 plunker fork 中回答和演示的那样,您可以在应用程序的某处创建一个外部 html 文件,只要您指定文件的正确路径,模态就会按预期工作.
  2. 由于您已经使用了链接到的 plunker,您可以将模板放入脚本 type="text/ng-template" 标记中并引用其 id 的值 模态配置中的属性.
  3. 最后一种方法是在模态配置中内联 html.这是您的 plunker 与直接添加到模态的 template 属性的 html 分叉配置为字符串.

    var modalInstance = $uibModal.open({模板:'<div class="modal-header">'+'<h3 class="modal-title">我是一个模态!</h3>'+'</div>'+'

这种方法工作得很好,但根据模板中的标记数量,编写、读取和维护可能有点麻烦.

I am using UI bootstrap to create modal dialog. It works fine if I use "templateUrl" and use "ng-template" to include the html template. But as I have multiple modal dialogs, my page is getting bigger and bigger after including all the html templates in the page using "ng-template".

Here's the code: HTML

<head>
  <script data-require="angular.js@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.min.js"></script>
  <script data-require="angular-animate@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular-animate.js"></script>
  <script data-require="ui-bootstrap@1.3.2" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script>
  <script src="script.js"></script>
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>

<body>
  <div ng-controller="myCtrl">
    <script type="text/ng-template" id="myContent.html">
      <div class="modal-header">
        <h3 class="modal-title">I'm a modal!</h3>
      </div>
      <div class="modal-body">
        Modal body content goes here...
      </div>
      <div class="modal-footer">
        <button class="btn" type="button" ng-click="cancel()">Close</button>
      </div>
    </script>

    <button type="button" class="btn btn-default" ng-click="open()">Show modal</button>
  </div>

</body>
</html>

Javascript:

angular.module('mydemo', ['ngAnimate', 'ui.bootstrap']);
angular.module('mydemo').controller('myCtrl', function ($scope, $uibModal, $log) {

  $scope.open = function (size) {

    var modalInstance = $uibModal.open({
      templateUrl: 'myContent.html',
      controller: 'ModalInstanceCtrl',
      size: size
    });
  };
});

angular.module('mydemo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance) {
  $scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
});

Is there any way I can use HTML markup directly instead of using "templateUrl"? Or atleast a way through which I can keep the html templates in a separate file instead of including it in the page using ng-template? Here's the working example: http://plnkr.co/edit/HqThAG79r22K2VGZSs2D?p=preview

解决方案

Yep, you have a few options for indicating which content the modal will load.

  1. As @Wes answered and demonstrated in his plunker fork, you can create an external html file somewhere in your application, and as long as you specify the correct path to the file, the modal will work as expected.
  2. As you already have working the the plunker you linked to, you can put the template inside a script type="text/ng-template" tag and reference the value of its id attribute in your modal config.
  3. A final way is to inline the html in the modal config. Here's your plunker forked with the html directly added to the template property of the modal config as a string.

    var modalInstance = $uibModal.open({
      template: '<div class="modal-header">' +
                  '<h3 class="modal-title">I\'m a modal!</h3>' +
                '</div>' +
                '<div class="modal-body">' +
                  'Modal body content goes here...' +
                '</div>' +
                '<div class="modal-footer">' +
                  '<button class="btn" type="button" ng-click="cancel()">Close</button>' +
                '</div>',
      controller: 'ModalInstanceCtrl',
      size: size
    });
    

This method works just fine, but it can be a little cumbersome to write, read, and maintain, depending on the amount of markup in the template.

这篇关于如何在 UI 引导模式中传递 HTML 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆