I am using ui-router and trying to create a widget instance that takes as a parameter the DOM element specified by id. This DOM element is in <div ng-switch>, and I want to call the constructor of the widget when the element is guaranteed to exist.
<div ng-switch on="state">
<div ng-switch-when="map">
<div id="map"></div>
</div>
</div>
From the ui-router life cycle , I understand that I have to connect to $viewContentLoaded. This, however, does not work - the DOM element inside is ng-switchnot created at this point:
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('/', {url: '/', templateUrl: 'index.html'})
.state('map', {url: 'map', templateUrl: 'map.html', controller: 'MapCtrl'})
}]);
app.controller('MapCtrl', ['$rootScope', '$scope', '$state', function MapCtrl($rootScope, $scope, $state) {
$scope.state = $state.current.name;
$scope.$on('$viewContentLoaded', function mapContentLoaded(event, viewConfig) {
var mapElement = document.getElementById('map');
console.log('Attempting to create map into', mapElement);
var map = new google.maps.Map(mapElement);
});
}]);
Which works using setTimeout()50 ms in the controller, which is fragile, but at this point a DOM element is created. Alternatively, I can set the interval, check for the presence of the mapDOM element, and clear the interval when it is found.
, ng-switch DOM? .
Plunkr.