你可以覆盖AngularUI Bootstrap中的特定模板吗?

Jer*_*ett 87 angularjs angular-ui angular-ui-bootstrap

我很好奇是否有办法覆盖ui-bootstrap-tpls文件中的单个特定模板.绝大多数默认模板都符合我的需求,但有一些我想要替换的特定模板,而不是通过抓取所有默认模板并将它们连接到非tpls版本的整个过程.

pko*_*rce 122

是的,来自http://angular-ui.github.io/bootstrap的指令是高度可定制的,并且很容易覆盖其中一个模板(并且仍然依赖于其他指令的默认模板).

它足以$templateCache直接输入(如ui-bootstrap-tpls文件中所做)或 - 可能更简单 - 使用<script>指令(doc)覆盖模板.

在那里我改变了警报的模板来交换一个人为的例子xClose如下所示:

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.4.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">

    <script id="template/alert/alert.html" type="text/ng-template">
      <div class='alert' ng-class='type && "alert-" + type'>
          <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>
          <div ng-transclude></div>
      </div>
    </script>
  </head>

  <body>
    <div ng-controller="AlertDemoCtrl">
      <alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">                     
        {{alert.msg}}
      </alert>
      <button class='btn' ng-click="addAlert()">Add Alert</button>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

直播员:http://plnkr.co/edit/gyjVMBxa3fToYTFJtnij?p =preview

  • 我喜欢这个答案.我只是不喜欢它没有包含在Angular UI的文档页面中,并且花了很长时间才弄清楚如何做一些像显示模态一样简单的事情. (19认同)
  • @BruceBanner文档和可靠的工作示例是Angular UI的两大失败.这个项目很棒,但它需要一些甜蜜的开发者喜欢. (2认同)

JcT*_*JcT 79

运用 $provide.decorator

使用$provide装饰指令,避免了需要直接与周围乱$templateCache.

相反,您可以像平常一样创建外部模板html,无论您喜欢什么名称,然后覆盖指令templateUrl以指向它.

angular.module('plunker', ['ui.bootstrap'])
  .config(['$provide', Decorate]);

  function Decorate($provide) {
    $provide.decorator('alertDirective', function($delegate) {
      var directive = $delegate[0];

      directive.templateUrl = "alertOverride.tpl.html";

      return $delegate;
    });
  }
Run Code Online (Sandbox Code Playgroud)

pkozlowski.opensource的plunkr:http://plnkr.co/edit/RE9AvUwEmKmAzem9mfpI?p=preview

(请注意,您必须将'Directive'后缀附加到您要装饰的指令名称.上面,我们正在装饰UI Bootstrap的alert指令,因此我们使用该名称alertDirective.)

由于您可能经常想要做的不仅仅是覆盖templateUrl,这为进一步扩展指令提供了一个很好的起点,例如通过覆盖/包装链接或编译函数(例如).

  • 这是正确的解决方案,遵循角度最佳实践.您不应该使用字符串来创建HTML,也不必将其显式包含在您注入第三方脚本的index.html文件中.谢谢@JcT! (9认同)
  • 当你注册时,angularjs` $ compileProvider`会在你的指令名称附加一个'Directive'后缀(`$ filterProvider`类似于'Filter'后缀); 在大多数情况下,这是不可见的,但在装饰时,您需要将此后缀附加到您要定位的指令.例如,`tabDirective`或`tabsetDirective`等.我找不到任何地方的文件并不完全清楚,但这里至少提到了`$ filterProvider`的类似行为:https://docs.angularjs.org/api/NG /供应商/ $ filterProvider (4认同)
  • 嗨,`alertDirective`是关键字吗?如果是,"标签"的关键字是什么?我试图在标签上做类似的事情,但我查看了alert.js,我没有看到他们在那里有`alertDirective`. (2认同)
  • 非常感谢@JcT,一个很好的答案.这是正确的方法.而且,正如你所说,第三方指令"装饰"的一个很好的起点:) (2认同)

Mat*_*rne 27

pkozlowski.opensource的答案非常有用,帮了我很多忙!我在我的条件下调整它,以便有一个文件定义我的所有角度模板覆盖并加载外部JS以保持有效负载大小.

为此,请转到角度ui-bootstrap源js文件的底部(例如ui-bootstrap-tpls-0.6.0.js)并找到您感兴趣的模板.复制定义模板的整个块并将其粘贴到覆盖JS文件中.

例如

angular.module("template/alert/alert.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("template/alert/alert.html",
     "      <div class='alert' ng-class='type && \"alert-\" + type'>\n" +
     "          <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>\n" +
     "          <div ng-transclude></div>\n" +
     "      </div>");
}]);
Run Code Online (Sandbox Code Playgroud)

然后在ui-bootstrap之后包含你的覆盖文件,你就可以获得相同的结果.

的分叉版本pkozlowski.opensource的普拉克http://plnkr.co/edit/iF5xw2YTrQ0IAalAYiAg?p=preview


小智 7

您可以使用template-url="/app/.../_something.template.html"覆盖该指令的当前模板.

(至少在手风琴引导程序中工作.)