简体   繁体   English

Angular和jQuery ng-included with document.ready无法正常工作

[英]Angular and jQuery ng-include with document.ready not working

I am trying to load a component which is placed in a separate html using HMTL. 我正在尝试使用HMTL加载一个放在单独的html中的组件。 The problem is that it is to be invoked as soon as the page is loaded in the browser. 问题是,只要在浏览器中加载页面就会调用它。

Below is my Modal Code: 以下是我的模态代码:

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

The component code is here: 组件代码在这里:

termsAndConditions.html 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>

The Way I am invoking the modal using JavaScript is: 我使用JavaScript调用模式的方式是:

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

The problem is: if I dont use ng-include this works fine. 问题是:如果我不使用 ng-include,这样可以正常工作。 But when I use ng-include it does not work . 但是当我使用ng-include时不起作用 I think that ng-include is not getting executed first and hence the modal is not getting loaded. 我认为ng-include不会先执行,因此模态不会被加载。 Is there any solution to this? 这有什么解决方案吗?

Thanks, Ankit 谢谢,Ankit

The modal dialog definitely needs to be initiated on some later event than document.ready . 模态对话框肯定需要在比document.ready晚的事件上启动。 It's just a question of deciding which is the best event. 这只是决定哪个是最佳事件的问题。

window.load is the most obvious event to try but is not a particularly "Angular" approach. window.load是最明显的尝试,但不是特别“Angular”的方法。

The earliest reliable event would be the completion of loading of the dialog div, and Angular provides for this with a $includeContentLoaded event. 最早的可靠事件是完成加载对话框div,Angular为此提供了$includeContentLoaded事件。

To demonstrate the principle, here's a demo with content loaded from a local template and with jQueryUI's .dialog() : 为了演示这个原理,这里是一个演示,其中包含从本地模板和jQueryUI的.dialog()加载的内容:

HTML 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>

Note that the ng-include and ng-controller directives work in consortium to achieve the objective of triggering an action when the content (determined by the the src attribute) has loaded 请注意, ng-includeng-controller指令在联盟中工作,以实现在内容(由src属性确定)加载时触发操作的目标

Javascript 使用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 的jsfiddle

There's still some work to do though not a lot. 虽然不是很多,但仍有一些工作要做。 Your final code should be largely a simplification of the above as you don't need the local template or the associated $rootScope.templates map. 您的最终代码应该主要是对上述代码的简化,因为您不需要本地模板或关联的$rootScope.templates映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM