【问题标题】:Angularjs not interpolating title for Bootstrap PopoverAngularjs没有为Bootstrap Popover插入标题
【发布时间】:2015-02-13 16:14:37
【问题描述】:

我正在尝试在 AngularJS ngRepeat 指令中使用 Bootstrap Popover。问题是标题属性没有被插值。我该如何解决这个问题?

查看

<div ng-app="app" id="ng-app">
<ul ng-controller="Main">
    <li ng-repeat="i in items" data-toggle="popover" title="{{ i.title }}" <!-- Title isn't interpolated so Popover doesn't render a title -->
        data-content="Events: {{ day.events.length }}"
        init-popover>{{ i.data }}</li>
</ul>

代码

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

app.controller('Main', ['$scope', function($scope){
    $scope.items = [
        { title: 'Title 1', data: 'lorem' },
        { title: 'Title 2', data: 'ipsum' },
    ];
}]);

app.directive('initPopover', function() {
  return function(scope, element, attrs) {
    if (scope.$last){
      $('[data-toggle="popover"]').popover({container: 'ul'});
    }
  };
});

FIDDLE

【问题讨论】:

  • 你好。我很欣赏你包括一个小提琴,但目前尚不清楚问题是什么。你能改进你的小提琴,让它说“这就是我想要发生的事情:X。如你所见,Y 正在发生。”
  • 对不起,我不清楚。我更新了小提琴。
  • 我会在这里建议ui-bootstrap,但请参阅我的答案以获取使用当前插件的解决方案。

标签: javascript angularjs twitter-bootstrap-3 popover


【解决方案1】:

你可以让它工作,但它很丑,这是我的解决方案

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

app.controller('Main', ['$scope', function($scope){
    $scope.items = [        
        { title: 'Title 1', data: 'Test in ngRepeat' },
        { title: 'Title 2', data: 'Test in ngRepeat' },
    ];
}]);

app.directive('initPopover', function() {
  return function(scope, element, attrs) {
      if (scope.$last) {
      attrs.$observe('title', function () {
          $('[data-toggle="popover"]').popover({container: 'ul'});
      });
      }
  };
});

【讨论】:

    【解决方案2】:

    你不应该使用title="{{i.title}}",而应该使用ng-attr-title="{{i.title}}"...

    【讨论】:

    • 对不起,你的小提琴我也不清楚:你的“期望结果”是什么?但是,要让 AngularJS 插入 title 属性,您必须使用ng-attr-title,而不能 title...
    • 想要的结果是让 Popover 的标题成为作用域对象中的一个值。它目前没有被插值。如果我尝试使用ng-attr-title,它不会在弹出窗口中呈现。我将更新小提琴以显示这一点。
    • 我明白了,抱歉,我没明白你的意思。抱歉,我从来没有使用过弹出框,所以在这里帮不了你...更多,我看到标题是由 AngularJS 和 ng-attr-title 插值的...我很确定前一段时间(和角度版本之前)事实并非如此...... :-(
    • Popover 需要设置 title 属性才能为 popover 呈现标题,但它只是没有插值。
    • 我记得,现在... :-) 当您想要 动态 标题(即:遵循模型的标题,而不是静态设置时,您必须使用 ng-attr-title页面加载...)。
    【解决方案3】:

    我遇到了同样的问题。我正在使用引导程序 v3.3.6 和 Angular v1.5.0

    添加属性 data-original-title="{{someTitle}}" 解决了这个问题。

    <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="{{someTitle}}" data-original-title="{{someTitle}}" data-content="Some Content">Click to toggle popover</button>
    

    另外,根据引导程序的要求,在指令中调用 .popover()

    module.directive('myDir', ['jQuery', function (jQuery) {
        return {
            link: function () {
                jQuery('[data-toggle="popover"]').popover();
            }
        };
    }]);
    

    【讨论】:

      【解决方案4】:

      $timeout 添加到指令将解决您的问题。 当 ng-repeat 的最后一个元素在 UI 上呈现并运行 $digest 循环时,将调用 $timeout。

      指令

      app.directive('initPopover', function($timeout) {
          return function(scope, element, attrs) {
              if (scope.$last) {
                  $timeout(function() {
                      angular.element('[data-toggle="popover"]').popover({
                          container: 'ul'
                      });
                  });
              }
          };
      });
      

      Working Fiddle

      希望这可以帮助你。谢谢。

      【讨论】:

        【解决方案5】:

        考虑将 ui-bootstrap 用于原生 Angular 指令,而不是 Bootstrap 的 jQuery 版本。在 Angular 项目中使用它们要容易得多。

        编辑:如果你不能这样做,这里有一个解决方案:

        <li ng-repeat="i in items" data-toggle="popover" data-ng-attr-popover_title="{{ i.title }}" 
                    data-content="Popover Content"
                    init-popover>{{ i.data }}</li>
        

        对指令稍作改动:

        app.directive('initPopover', function() {
          return function(scope, element, attrs) {
            if (scope.$last){
                $('[data-toggle="popover"]').popover({ container: 'ul', title: attrs.popoverTitle });
            }
          };
        

        ng-attr-popup_title 将在元素上创建一个名为 popupTitle 的 HTML 属性,其中 {{}} 中的 Angular 语法已正确解析。然后,在指令中,我们可以检查该属性来设置弹出框标题。

        小提琴:here

        【讨论】:

          【解决方案6】:

          只是在这里添加到知识库。这是有角度的方式,以防万一有人需要。我的解决方案基于Mark Coleman's PopOver tutorial,当然还有 Nate Barbettini 贡献的内容。

          我的情况是我在项目中的项目上有一个嵌套的 ng-repeat,所以我需要为我的弹出窗口设置一个标题,该标题基于存储在范围中的数组,而弹出窗口的内容来自另一个存储的数组在作用域的数组中。

          我在我的 HTML 元素中添加了ng-attr-title,如下所示:

           <div pop-over items='week.items' ng-attr-title="{{'Week number:'+ week.week}}">  Total sales this week : {{week.sales}}</div>
          

          然后,在我的指令中,我主要遵循 Mark Coleman 的做法。

              angular.module('vegadirectives', [])
          .directive('popOver', function ($compile, $templateCache) {
          
          var itemsTemplate = "<ul class='popper'><li ng-repeat='thing in items track by $index'>";
          itemsTemplate+=" <i class='fa fa-square' ng-class=";
          itemsTemplate+="{purple:thing.color==='purple','orange':thing.color==='orange','white':thing.color==='white','pink':thing.color==='pink','red':thing.color==='red','green':thing.color==='green','yellow':thing.color==='yellow'}";
          itemsTemplate+="></i>  {{thing.model}} - {{thing.qty}}</li></ul>";
              var getTemplate = function () {
                  var template=itemsTemplate;
                  return template;
              }
              return {
                  restrict: "A",
                  transclude: true, 
                  template: "<span ng-transclude></span>",
                  link: function (scope, element, attrs) {
                      var popOverContent;
          
          
                      if (scope.items) {
                          var html = getTemplate();
                          popOverContent = $compile(html)(scope);                    
                          var options = {
                              trigger:'click',
                              content: popOverContent,
                              placement: "right",
                              html: true,
                              title:scope.title
          
          
                          };
                          if (scope.$last) {
          
                          }
                          $(element).popover(options);
                      }
                  },
                  scope: {
                      items: '=',
                     title:'@'
                  }
              };
          });
          

          解释:

          为了用一个项目列表填充我的弹出框,用一些花哨的颜色方块来指示不同类型的项目,我创建了一段 HTML 代码。 $compile 将使用这个名为 itemsTemplate 的块来创建具有范围的弹出内容。我认为用法是$compile(htmlstring)(scope)。

          接下来是弹出框选项设置的标准内容。 对于带标题的范围插值,我指定了选项title:scope.title。然后,在最后的scope: 选项中,使用'=''@' 指定两个范围元素。

          所以,title:scope.title 引用或绑定到scope: {items:'=', title:'@' }title:'@' 是 HTML 中标题的属性,即ng-attrs-title={{week.week}},其中周来自外部 ng-repeat 循环。

          希望对您有所帮助。

          【讨论】:

            【解决方案7】:

            https://stackoverflow.com/a/17864868/2379762 找到这个答案后,我尝试了 data-original-title={{i.title}},它有效。

            http://jsfiddle.net/f1tjj8x5/

            <li ng-repeat="i in items" data-toggle="popover" data-original-title="{{ i.title }}" 
                    data-content="Popover Content"
                    init-popover>{{ i.data }}</li>
            

            【讨论】:

              猜你喜欢
              • 2013-10-23
              • 2014-12-21
              • 1970-01-01
              • 1970-01-01
              • 2012-11-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多