【问题标题】:Bootstrap popover not working inside AngularJs ng-repeatBootstrap 弹出框在 AngularJs ng-repeat 中不起作用
【发布时间】:2023-03-04 09:22:01
【问题描述】:

我有一个国家/地区列表,我正在使用 ng-repeat 填充这些国家/地区列表,一切正常。另外,我正在尝试通过使用引导弹出窗口来显示每个国家/地区的其他详细信息,但它似乎不起作用,所以有人可以帮助我吗?

HTML代码:

<body ng-app="app" ng-controller="my_controller">    
<div class="span3 projectCard" ng-repeat="country in all_countries">
    <div class="projectCardHeight">
        <button class="btn close cardClose" ng-click="deleteCountry(country.id)">×</button>
        <div class="cardHeader">Country</div>
        <div class="cardBody">{{country.id}}</div>
        <div class="cardBody">{{country.title}}</div>
        <div class="cardBody"><a href="#" id="pop{{country.id}}" class="btn btn-primary" rel="popover" data-content="This is the <b>body</b> of Popover" data-original-title="Creativity Tuts">pop</a></div>
    </div>
</div>
 </body>

Javascript 代码:

var app = angular.module("app", []);
app.controller('my_controller', function($scope) {  
    $scope.all_countries = [
        {"id":28,"title":"Sweden"},
        {"id":56,"title":"USA"},
        {"id":89,"title":"England"}];
});

function deleteCountry(id)
{
    alert("Do something");
}

$(document).ready(function () {
    $("a[rel=popover]")
        .popover({ placement: 'bottom', html: 'true' })
        .click(function (e) {
            e.preventDefault();
        });
});

请参考这个JsFiddle

【问题讨论】:

  • 参考引导文档并使用 selector 与使用 jQuery .on() 相同,以便委托事件处理程序,或将代码包装在指令中
  • 你有同样的例子吗?
  • 查看 jQuery on() 文档,或此处有关事件委托的数千篇文章之一
  • 好的。将检查相同。

标签: angularjs twitter-bootstrap angularjs-ng-repeat


【解决方案1】:

演示:http://jsfiddle.net/5ww8e/1/

创建一个名为bs-popover 的新指令并将其与ng-repeat 一起应用。在bs-popover 指令中触发popover 方法。

另外你的js文件加载顺序错误,你应该在bootstrap之前加载jquery。

【讨论】:

  • 由于某种原因,它在我的页面中不起作用,所以在 ng-repeat 中我使用 ng-click 而不是指令,现在它可以工作了。
  • 当我点击另一个弹出框(弹出按钮)时弹出框不会自动关闭。一次只需要打开弹出框。
  • 这对我有用 app.directive('bsPopover', function() { return function(scope, element, attrs) { element.find("a[rel=popover]").popover( { 位置:'底部',html:true}); }; });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2023-03-28
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多