【问题标题】:Angular and jQuery ng-include with document.ready not workingAngular和jQuery ng-include与document.ready不起作用
【发布时间】:2015-04-21 20:37:39
【问题描述】:

我正在尝试加载一个使用 HMTL 放置在单独 html 中的组件。问题是,一旦页面在浏览器中加载,它就会被调用。

以下是我的模式代码:

<div class="modal fade borderColorC0C0C0 borderRadiusOverride" id="termsAndConditionsPopover" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false" ng-include="'components/popover/termsAndConditions/termsAndConditions.html'">

</div>

组件代码在这里:

termsAndConditions.html

<div class="modal-dialog borderRadiusOverride">
    <div class="modal-content borderRadiusOverride">
      <div class="termsAndConditionsHeaderColor borderRadiusOverride divHeight50 paddingTop15 paddingLeft15 paddingBottom15 borderBottomColorC0C0C0">
        <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>-->
        <h5 class="modal-title marginBottom15 fontColorTileSteps" id="myModalLabel">Cisco's GSA shipping Policy</h5>
      </div>
      <div class="modal-body borderRadiusOverride fontColorTileSteps">
        This policy outlines the requirements of shipping Internationally including but not limited to:
<ul>
    <li>All members of the Cisco workforce are responsible to adhere to this policy</li>
    <li>AST is to not be used for personal shipments</li>
    <li>Prohibited items</li>
    <li>Textiles</li>
    <li>Shipments to Trade shows, hotels, residential addresses</li>
    <li>Importer of record requirements</li>
    <li>Shipment of used equipment</li>
    <li>Other restrictions; including export requirements</li>
</ul>
<p>Fixed Assets shipping from one Cisco entity to another Cisco entity must transfer the value to the receiving entity. It is the responsibility of the person initiating the shipment to ensure this takes place. Please refer to the Asset Management System. AMS is used in US, India, China and Brazil. The asset tracking process will vary for the rest of the countries.</p>

<p><strong>PLEASE NOTE:</strong> The person initiating the shipment is responsible for the accuracy of all shipment information. Should fines be levied due to misinformation given by individual, all associated costs will be charged to your Department.</p>

<p>In International transactions governmental agencies have the power to request evidence to attest to the information given on commercial documentation, either at importation or in subsequent audits. International shipments may be subject to export and/or import licenses. The recipient of the international shipment may be required to obtain import licensing based on the destination country or address (business/residential) or the goods contained within the shipment.</p>
      </div>
      <div class="textAlignCenter borderRadiusOverride">
        <!--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button> -->
        <button type="button" class="btn btn-primary buttonColor525252 termsAndConditionsButton marginTop10 marginBottom30 fontColorWhite" data-dismiss="modal">I have read and I comply with Cisco's GSA shipping Policy</button>
      </div>
    </div>
  </div>

我使用 JavaScript 调用模式的方式是:

$(document).ready(function(e) {
    $('#termsAndConditionsPopover').modal('show');
});

问题是:如果我不使用 ng-include,这可以正常工作。但是当我使用 ng-include 时,它不起作用。我认为 ng-include 没有首先执行,因此模式没有被加载。有什么解决办法吗?

谢谢, Ankit

【问题讨论】:

  • 试试$(window).load(fn)
  • 有一个$includeContentLoaded 事件,但不幸的是没有特别好的记录。您可能需要在附加到该事件的处理程序中启动 .modal()

标签: javascript jquery angularjs document-ready angularjs-ng-include


【解决方案1】:

模态对话框肯定需要在document.ready 之后的某个事件上启动。这只是决定哪个是最好的事件的问题。

window.load 是最明显的尝试,但不是特别“Angular”的方法。

最早的可靠事件是对话框 div 的加载完成,Angular 提供了一个$includeContentLoaded 事件。

为了演示原理,这里有一个从本地模板加载内容并使用 jQueryUI 的 .dialog() 的演示:

HTML

<body ng-app="demo">
    <div ng-controller="AppController">
        <script type="text/ng-template" id="modal.html">
            <p>This is included text</p>
        </script>
        <div id="termsAndConditionsPopover" ng-include src="templates.modal" ng-controller="ModalCtrl"></div>
    </div>
</body>

请注意,ng-includeng-controller 指令在联盟中工作,以实现在内容(由 src 属性确定)已加载时触发操作的目标

Javascript

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

demo.controller('AppController', ['$rootScope', function ($rootScope) {
    $rootScope.templates = {
        modal: 'modal.html'
    };
}]);

demo.controller('ModalCtrl', ['$scope', function ($scope) {
    $scope.$on('$includeContentLoaded', function(event) {
        $('#termsAndConditionsPopover').dialog({ autoOpen: true });
    });
}]); 

jsFiddle

还有一些工作要做,虽然不是很多。您的最终代码应该在很大程度上简化了上述代码,因为您不需要本地模板或关联的 $rootScope.templates 映射。

【讨论】:

  • 非常好,乐于助人。
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 2018-03-27
  • 2014-11-30
  • 2016-01-20
  • 2018-01-20
  • 2015-02-20
  • 2014-01-22
相关资源
最近更新 更多