简体   繁体   English

如何防止在对象完全加载到$ scope中之前加载DOM?

[英]How to prevent DOM from loading until object is fully loaded into $scope?

I am loading a JSON that is almost 19,000 lines...minified to a single line its about 240k. 我正在加载将近19,000行的JSON ...最小化为一行约240k的行。 I am loading it into my app via index.html file as a global var: 我通过index.html文件将其作为全局变量加载到我的应用程序中:

<script type="text/javascript" src="js/cdjson.js"></script>

var _countries = {
   "country": {
      "USA": {
         "currency": {
            "currencyCode":["USD"],
                "currencyName":["United States Dollar"],
                "currencySymbol":["&#36;"]
         },
         "info": { 
               ...
               ...
         },
         "phone" : {
               ...
         },
         "postal" : {
               ...
         }
      },
      "CAN" : {
         ...
      }
   }
}  

In the the controller its being assigned to a $scope variable $scope.countries = _countries.country ; 在控制器中,将其分配给$ scope变量$scope.countries = _countries.country ; . However, when the html DOM for that controller loads its trying to access the $scope variable before before the object is fully loaded into the $scope causing JS object errors like Cannot read property 'country' of undefined . 但是,当该控制器的html DOM在对象完全加载到$ scope中之前加载其尝试访问$ scope变量的尝试时,会导致JS对象错误,例如Cannot read property 'country' of undefined

How can I prevent the page from rendering until the $scope object is fully loaded? 如何在$ scope对象完全加载之前阻止页面呈现? The $scope country object is being used in a <select ng-repeat="countries as country"> $ scope国家对象在<select ng-repeat="countries as country">

Have a flag that indicates when the data was fully loaded to the controller. 有一个标志,指示何时将数据完全加载到控制器。 Something like 就像是

<select ng-if="countries.length" ng-repeat="countries as country">

Ideally your js files should be loaded at the end of your HTML so you dont have this problem. 理想情况下,应该在HTML的末尾加载js文件,这样就不会出现此问题。 But if that is not an option for you then add this in your controller 但是,如果这不是您的选择,则将其添加到控制器中

$scope.countries = []
angular.element(document).ready(function () {
    $scope.countries = _countries.country;
});

and in your HTML 并在您的HTML中

<select ng-if="countries.length" ng-repeat="countries as country">    

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

相关问题 如何防止在所有脚本完全加载之前单击图像 - how to prevent clicking on an image until all the scripts is fully loaded 如何在标签内容完全AJAX加载之前显示加载消息? - How to display a loading message until the tab content is fully AJAX loaded? 显示正在加载图像直到div完全加载 - Show Loading image Until div fully loaded 隐藏对象直到页面完全加载 - Hide the object until the page is fully loaded 如何强制Puppeteer等到非常大的HTML表中的所有行完全加载并显示在DOM中 - How to force Puppeteer to wait until all rows in very large HTML table are fully loaded and displayed in the DOM 防止浏览器在完全加载之前显示图像的最佳方法是什么? - What's the best way to prevent a browser from displaying images until they are fully loaded? 如何在下一页内容完全加载之前显示加载或进度条? - How to show loading or progress bar until the next page content fully loaded? 如何在没有Ajax Toolkit的情况下完全加载gridview之前显示加载图像? - How to display a loading image until a gridview is fully loaded without Ajax Toolkit? "如何等到页面完全加载" - How to Wait until page is fully loaded 在内容完全加载后尝试阻止功能 - Trying to prevent function until after content is fully loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM