在我看來沒有更新我有這樣的:
<div class="tab-loading-container" ng-if="mapStatus.loading =='true'">
<div class="tab-loading">Loading map</div>
</div>
在我的控制,我有:
$scope.mapStatus = {};
,然後將該更新使用範圍VAR各種功能由ng-if
,當滿足一定的條件,如等離線是(例如:
function enableMap() {
$scope.mapStatus.loading = false;
}
我的問題是儘管範圍var正在得到正確更改(通過良好的'console.log和角度chrome擴展名確認),視圖中的ng-if
從不更新/被添加/刪除以顯示/刪除div。
我使用$應用(雖然我對它的理解是不是很大)嘗試,例如:
function enableMap() {
$scope.$apply(function() {
$scope.mapStatus.loading = false;
});
}
,但拋出的錯誤,如Error: [$rootScope:inprog] $apply already in progress
覺得我失蹤明顯的東西:(
更多的代碼的要求:
angular.module('app.case.controller', [])
.controller('CaseController', function($rootScope, $scope, $state, $stateParams, $filter, casesFactory, $ionicActionSheet, $ionicModal, $ionicTabsDelegate, $ionicLoading, ConnectivityMonitor) {
/// Other non related code
// location map - refactor into a factory
$scope.mapStatus = {};
function initMap() {
var pos = { 'lat': 52.6136149, 'lng': -1.1936672 };
var latLng = new google.maps.LatLng(pos.lat, pos.lng);
var mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
fullscreenControl: false,
mapTypeControl: false
};
$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);
google.maps.event.trigger(map, 'resize');
//Wait until the map is loaded
google.maps.event.addListenerOnce($scope.map, 'idle', function() {
enableMap();
console.log('map loaded');
var marker = new google.maps.Marker({
map: $scope.map,
animation: google.maps.Animation.DROP,
position: latLng
});
google.maps.event.trigger(map, 'resize');
$scope.map.setCenter(latLng);
});
}
function loadGoogleMaps() {
$scope.mapStatus.loading = true;
// This function will be called once the SDK has been loaded
window.mapInit = function(){
initMap();
};
// Create a script element to insert into the page
var script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'googleMaps';
script.src = 'https://maps.googleapis.com/maps/api/js?key=XXX&callback=mapInit';
document.body.appendChild(script);
}
function enableMap() {
$scope.mapStatus.loading = false;
$scope.mapStatus.offline = false;
}
function disableMap() {
$scope.mapStatus.offline = true;
}
function checkLoaded() {
if (typeof google == 'undefined"' || typeof google.maps == 'undefined') {
loadGoogleMaps();
} else {
enableMap();
}
}
function addConnectivityListeners() {
if (ionic.Platform.isWebView()) {
// Check if the map is already loaded when the user comes online, if not, load it
$rootScope.$on('$cordovaNetwork:online', function(event, networkState) {
checkLoaded();
});
// Disable the map when the user goes offline
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState) {
disableMap();
});
} else {
//Same as above but for when we are not running on a device
window.addEventListener("online", function(e) {
checkLoaded();
}, false);
window.addEventListener("offline", function(e) {
disableMap();
}, false);
}
}
function showMap() {
console.log('showMap() called');
if (typeof google == 'undefined' || typeof google.maps == 'undefined') {
console.warn("Google Maps SDK needs to be loaded");
disableMap();
if (ConnectivityMonitor.isOnline()){
loadGoogleMaps();
}
}
else {
if (ConnectivityMonitor.isOnline()){
initMap();
enableMap();
} else {
disableMap();
}
}
addConnectivityListeners();
}
$scope.initMap = function() {
showMap();
};
要確認範圍瓦爾正在這裏改變是從AngularJS Chrome擴展程序截圖:
你爲什麼用** **字符串''true''一個比較** **布爾值,只是把它寫爲'NG-IF = 「mapStatus.loading」' – George
哎呦,好點。但是,在更改爲'mapStatus.loading'之後,即使控制器中的var被設置爲false(當滿足某些條件時),div也會始終顯示。 –
我想你應該使用'ng-show'而不是'ng-if' – Xatyrian