ajax - 使用 Angular 和 Ajax 将内容加载到 Bootstrap 弹出窗口中不起作用

标签 ajax angularjs twitter-bootstrap

我正在尝试使用 Angular 和 Ajax 将内容填充到 Bootstrap 弹出窗口中。尽管数据已正确加载(正如我在 console.log 中看到的那样),但结果不会出现在弹出窗口的内容中。我认为当 Bootstrap 弹出窗口加载时, Angular 的加载尚未准备好。因此,如何在 Angular 加载后立即填充此弹出窗口?

这是代码:

$(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>

提前致谢

最佳答案

您可以将函数放入 Controller 中,以获得 Angular 周期。并且,还定义对 $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
    });
  });

}

关于ajax - 使用 Angular 和 Ajax 将内容加载到 Bootstrap 弹出窗口中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119693/

相关文章:

javascript - UI 网格 - 水平滚动不适用于大量数据

css - 如何在 Bootstrap 中为 jumbotron 添加背景图像?

css - Bootstrap 4 CSS - 调整内容中心问题

javascript - 更改 html 后的 jQuery AJAX 后退按钮

python - 需要用ajax调用实现django-filter

javascript - 如何将键分配给值数组以传递给 API?

javascript - Angular 1 : How to reflect ng-model errors on another element?

css - html 中的图像不能与 Bootstrap 列重叠吗?

javascript - 在 .click 上使用 $.ajax 更新变量

javascript - 使用ajax加载onsen-ui内容