【问题标题】:Angular UI Bootstrap: Dynamic popover template IDAngular UI Bootstrap:动态弹出框模板 ID
【发布时间】:2016-05-31 23:21:33
【问题描述】:

Angular UI Popover 是否可以使用带有动态 ID 的模板?

例如:

<div ng-repeat="row in data">
    <button class="btn" uib-popover-template="'myTemplate_{{row.id}}.html'" popover-trigger="mouseenter">View</button>
    <script type="text/ng-template" id="myTemplate_{{row.id}}.html">
        <strong>Name</strong>: {{row.name}}<br/>
        <strong>Phone</strong>: {{row.phone}}<br/>
        <strong>Email</strong>: {{row.email}}
    </script>
</div>

当我将鼠标悬停在按钮上时,Angular 会尝试从服务器获取模板(例如 myTemplate_1.html),而不是使用 ng-template。

我接近这个了吗?如果没有,将动态 html 内容分配给弹出框的最佳方法是什么?

谢谢。

【问题讨论】:

    标签: angularjs angular-ui-bootstrap angular-ui


    【解决方案1】:

    Id 需要是唯一的,任何通过uib-popover-template 传递的数据都会被解析。 id=myTemplate_{{row.id}} 中的数据未被解释。

    试试这样的:

    angular.module('test', [])
      .controller('GreetingController', ['$scope',
        function($scope) {
          $scope.data = [{
            name: 'John'
          }, {
            name: 'Bill'
          }];
        }
      ]);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="test" ng-controller="GreetingController">
      <div ng-repeat="row in data">
        <button class="btn" uib-popover-template="'myTemplate_row.html'" popover-trigger="'mouseenter'">View</button>
      </div>
    </div>
    
    <script type="text/ng-template" id="myTemplate_row.html">
      <strong>Name</strong>: {{row.name}}
      <br/>
      <strong>Phone</strong>: {{row.phone}}
      <br/>
      <strong>Email</strong>: {{row.email}}
    </script>

    注意示例不起作用,因为我没有 uib 模块,但基本原理是相同的,应该会显示出来。

    【讨论】:

    • 您必须将模板和触发字符串放在单引号中才能使其工作&lt;button class="btn" uib-popover-template="'myTemplate_row.html'" popover-trigger="'mouseenter'"&gt;View&lt;/button&gt;
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2018-07-26
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多