【问题标题】:Load content into bootstrap popover using angular and ajax don't work使用 angular 和 ajax 将内容加载到引导弹出窗口不起作用
【发布时间】:2015-11-14 04:20:42
【问题描述】:

我正在尝试使用 angular 和 ajax 将内容填充到引导弹出窗口中。尽管数据已正确加载(正如我在 console.log 中看到的那样),但结果不会出现在弹出框的内容中。我认为加载引导弹出窗口时,角度的加载还没有准备好。因此,如何在角度加载后立即填充此弹出框?

这是代码:

$(function() {
  var p = $("#popover").popover({
    content: $("#popover-content").html(),
    html: true
  });

});

(function () {
  var app = angular.module("ng-app", []);

  app.controller("NotificationsController", function ($scope, $http) {
    $scope.links = [];
    $scope.load = function () {
      $http.get("json.php").success(function (response) {
        console.log(response);
        $scope.links = response;

        // the response return is somethin like
        // [
        //    {title: "Google", url: "http://www.google.com"},
        //    {title: "Gmail", url: "http://www.google.com"},
        // ]

      });
    };
    $scope.load();
  });
})();
<ul class="nav navbar-nav">
  <li data-ng-controller="NotificationsController as notification">
    <a href="#" data-toggle="popover" id="popover" title="Notificações" data-placement="bottom" data-trigger="focus">
      <span class="badge">{{links.length}}</span>
    </a>

    <div id="popover-content" style="display: none">
      <div data-ng-repeat="link in links">
        <a href="{{link}}">{{link.title}}</a>
      </div>
    </div>
  </li>
</ul>

提前致谢

【问题讨论】:

  • 我遇到了同样的问题,我在这里创建了一个问题/问题...请阅读此内容,并尝试适应您的需求:stackoverflow.com/questions/27131078/…
  • 不幸的是,你的建议对我不起作用@JoaozitoPolo
  • 我觉得我的解决方案有点复杂。我今天写了一个新的解决方案,在我的本地项目上,我会尝试在你的代码中应用。

标签: ajax angularjs twitter-bootstrap


【解决方案1】:

您可以将您的函数放在控制器内部,以获得角度周期。此外,在 $timeout 事件上定义您的 jquery 调用。

请在 jsbin 上查看此示例:jsbin

controller('c', function($scope, $timeout) {

  $scope.getData = function() {
    return $("#popover-content").html();
  };

  $timeout(function() {
    $("#popover").popover({
      content: $scope.getData,
      html: true
    });
  });

}

【讨论】:

  • 我将内容按功能用于popover
猜你喜欢
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
相关资源
最近更新 更多