我有一个带有templateUrl
的指令。在指令中,有一个$ scope变量初始化为
$scope.mapLoaded = false;
和方法
$scope.onMapLoaded = function(){
$scope.mapLoaded = true;
$scope.$apply();
};
在$scope.init()
函数中,我正在检查是否已加载地图,并调用方法initAutocomplete()
(此方法将初始化Google地址API)
$scope.init = function()
{
console.log('init() is called ' + ' at '+ Date.now());
if($scope.applicationId === undefined || $scope.applicationId == '') return false;
$scope.getAssetLocations($scope.loadAssetLocations);
$scope.getAccountId($scope.loadAccountId);
if($scope.mapLoaded === true){
console.log('[$scope.init] >Map loaded is '+ $scope.mapLoaded + ' at '+ Date.now());
$timeout( function(){
$scope.initAutocomplete();
}, 4000 );
}
};
我也在看$scope.mapLoaded
变量
$scope.$watch('mapLoaded', function () {
console.log('[$watch(mapLoaded)] >Map loaded is '+ $scope.mapLoaded + ' at '+ Date.now());
if($scope.mapLoaded === true){
$timeout( function(){
$scope.initAutocomplete();
}, 4000 );
}
});
在视图部分中,我正在通过jQuery检查是否已准备好文档,然后加载google地图。
<html>
..some html here
</html>
<script>
$(document).ready(function () {
console.log('loadGoogleMap is called from ApplicationAsset at '+ Date.now() + ' after the document is ready');
window.setTimeout(loadGoogleMap, 1000);
});
function loadGoogleMap(){
if (window.google && document.getElementById('AppAssetLocation')) {
angular.element(document.getElementById('AppAssetLocation')).scope().onMapLoaded();
console.log('loadGoogleMap called from ApplicationAsset at '+ Date.now());
}
else {
window.setTimeout(loadGoogleMap, 1000);
console.log('Again, loadGoogleMap is trying to call from ApplicationAsset at '+ Date.now());
}
}
</script>
但是,这里发生的情况有时是在window.setTimeout(loadGoogleMap, 1000);
内部调用$(document).ready
,有时没有调用。而且是完全随机的。
这里的人们有什么问题?
答案 0 :(得分:0)
我使用
angular.element(document).ready(function () {// Code here});
代替
$(document).ready(function () { // Code here });
这解决了我的问题。