ngRepeat内部的Bootstrap Popover

时间:2014-06-11 18:36:31

标签: javascript jquery angularjs twitter-bootstrap

我一直在搜索网络并绞尽脑汁,但似乎无法找到解决方法。我需要在一个ng-repeat内部制作一个popover,其中popover内部也会有一个ng-repeat。

这是我到目前为止的JSFiddle,但是使用" phone.friends"不起作用:

http://jsfiddle.net/grzesir/Lq8ve/4/

HTML:

<div ng-app="AngularApp">
<div class="container" ng-controller="MainController">
    <div ng-repeat="phone in phones">

        {{phone.name}} 
        <a href="javascript: void(0);" class='repeatpopover' data-popover="true" data-html=true data-content="<div ng-repeat='friend in phone.friends'>{{friend.name}}</div>">hover here</a>

    </div>
</div>

使用Javascript:

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

angularApp.controller('MainController', function ($scope) {
    $scope.phones = [{
        'name': 'Nexus S',
            'snippet': 'Fast just got faster with Nexus S.',
            'friends': [{
            'name': 'John'
        }, {
            'name': 'Mike'
        }]
    }, {
        'name': 'Motorola XOOM™ with Wi-Fi',
            'snippet': 'The Next, Next Generation tablet.',
            'friends': [{
            'name': 'John 2'
        }, {
            'name': 'Mike 2'
        }]
    }, {
        'name': 'MOTOROLA XOOM™',
            'snippet': 'The Next, Next Generation tablet.',
            'friends': [{
            'name': 'Chris'
        }, {
            'name': 'Noah'
        }]
    }];
});

$(function () {
    $(".repeatpopover").popover({
        trigger: "hover"
    });
});

2 个答案:

答案 0 :(得分:4)

我已经使用过滤器更新了您的解决方案。

添加:

angularApp.filter('friendsHTML', function() {
  return function(input) {
      var html = '';
      for (idx in input) {
          html += '<div>' + input[idx].name + '</div>';
      }
      return html;
  };
});

然后在data-content参数的模板中执行data-content="{{ phone.friends | friendsHTML }}"。尽管如此,这可能会更加通用/可重复使用。

This也值得研究。希望有所帮助!

答案 1 :(得分:2)

如果有人感兴趣,这里有一个JS小提琴,它起作用:

http://jsfiddle.net/grzesir/TZ72k/2/

使用Javascript:

var angularApp = angular.module("bootstrap", []);

angularApp.controller('MainController', function ($scope) {
    $scope.phones = [{
        'name': 'Nexus S',
            'snippet': 'Fast just got faster with Nexus S.',
            'friends': [{
            'name': 'John'
        }, {
            'name': 'Mike'
        }]
    }, {
        'name': 'Motorola XOOM™ with Wi-Fi',
            'snippet': 'The Next, Next Generation tablet.',
            'friends': [{
            'name': 'John 2'
        }, {
            'name': 'Mike 2'
        }]
    }, {
        'name': 'MOTOROLA XOOM™',
            'snippet': 'The Next, Next Generation tablet.',
            'friends': [{
            'name': 'Chris'
        }, {
            'name': 'Noah'
        }]
    }];
});

angularApp.directive('popOver', function ($compile, $templateCache) {
        var getTemplate = function () {
            $templateCache.put('templateId.html', 'This is the content of the template');
            console.log($templateCache.get("popover_template.html"));
            return $templateCache.get("popover_template.html");
        }
        return {
            restrict: "A",
            transclude: true,
            template: "<span ng-transclude></span>",
            link: function (scope, element, attrs) {
                var popOverContent;
                if (scope.friends) {
                    var html = getTemplate();
                    popOverContent = $compile(html)(scope);                    
                    var options = {
                        content: popOverContent,
                        placement: "bottom",
                        html: true,
                        title: scope.title
                    };
                    $(element).popover(options);
                }
            },
            scope: {
                friends: '=',
                title: '@'
            }
        };
    });

HTML:

<div ng-app="bootstrap" id="example" ng-init="items = ['car', 'truck', 'plane', 'bike']">
    <div ng-controller="MainController">
        <div ng-repeat="phone in phones">
            <a href="#" pop-over friends="phone.friends", title="Mode of transport">Show Pop over</a>
        </div>
    </div>
    <script type="text/ng-template" id="popover_template.html">
        <ul class='unstyled'><li ng-repeat='friend in friends'>{{friend.name}}</li></ul>
    </script>
</div>