I've noticed that most questions relating to this subject are with regards to an alternative for the jQuery $(document).ready
function in angular, which is angular.element($document).ready
however i want a testable/best practice alternative to this.
I am currently injecting Bing Maps, which needs to have loaded before the code in my controller is executed.
currently i wrap the controller code in the document ready:
angular.element($document).ready(function() {
self.map = new Microsoft.Maps.Map(document.getElementById('map'), {
credentials: $scope.credentials,
enableClickableLogo: false,
enableSearchLogo: false,
showDashboard: false,
disableBirdseye: true,
allowInfoboxOverflow: true,
liteMode: true,
minZoom: 2
});
$scope.$watch('zoom', function (zoom) {
self.map.setView({animate: true, zoom: zoom});
});
if ($scope.onMapReady) {
$scope.onMapReady({ map: self.map });
}
});
Which works, but i'm unable to test it, so i assume this is incorrect usage.
I tried setting a variable in the directive, of $scope.loaded = true;
as i read that if the directive link function is hit the DOM must be loaded. I then tried replacing the document ready with:
$scope.$watch('loaded', function () {
self.map = new Microsoft.Maps.Map(document.getElementById('map'), {
credentials: $scope.credentials,
enableClickableLogo: false,
enableSearchLogo: false,
showDashboard: false,
disableBirdseye: true,
allowInfoboxOverflow: true,
liteMode: true,
minZoom: 2
});
if ($scope.onMapReady) {
$scope.onMapReady({ map: self.map });
}
});
$scope.$watch('zoom', function (zoom) {
self.map.setView({animate: true, zoom: zoom});
});
the 'loaded' watch works as expected but naturally the zoom is hit on load and thats before the map is set. I feel like i could change the document ready to a $timeout
function but that seems to be a workaround rather than the correct solution, is there a best practice alternative to angular.element($document).ready
that works in the same way but allows me to test it's contents successfully?