[英]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":["$"]
},
"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.