Pre compile a directive for use in Google Maps marker causes '$apply already in progress' error

I am creating an AngularJS app that integrates with google maps. I have some html to use as an info popup. I use a similar chunk of html elsewhere in my app, so I started using a directive to create a re usable component.

Once I had implemented this I realised that the info popup windows need to be initialised with their html at time of creation not at time of 'popping up'.

I investigated how to pre compile a directive. It was much the same as the AngularJS docs on testing a directive.

var element = angular.element("<my-directive data=data></my-directive>"); var scope = $rootScope.$new(); scope.data = someData; element = $compile(element)(scope); scope.$apply(); element.html(); // Gives me the html

This causes the error '$apply already in progress' I know that it means that a digest cycle is already in progress but this $apply is necessary as I need to create this precompiled template to pass to my info popup.

I have looked into $interpolate and $parse but I don't think I can use these as my directive has link functions that need to be evaluated.

-------------Problems Reply------------

Use

if (!$scope.$$phase)
$scope.$apply();

You can do safeapply(see below code), but its not recommended

https://coderwall.com/p/ngisma/safe-apply-in-angular-js

$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};

Category:javascript Views:0 Time:2016-10-24

Related post

  • What's the secret to hosting custom KML for use in Google Maps Mobile? 2010-07-14

    I am new to Android and so I may be missing some very basic things here. I am trying to host custom KML files on a server behind my firewall and display those KML files on the Android emulator. I started by writing a simple app that uses an Intent to

  • How do I convert from this weird Coordinate unit to LngLat for use in Google Maps? 2011-03-17

    I have a database full of rows if coordinate pairs like this: ux: 6643641 uy: 264274 uz: NULL I have been tasked to make all these coordinates appear on google maps as points of interest, but nobody could tell me what the hell those coordinates were.

  • How to install Google Inc. add-ons (dl-ssl.google.com) (Third party add-ons) for use with google maps external library? 2012-01-08

    I want to use com.google.android.maps.MapView, but can't figure out how to download the library for this. I have been looking at http://developer.android.com/sdk/adding-components.html. But in my SDK manager I simply can't find the section "third par

  • Google Map Marker Clickable Area 2011-11-08

    I am using this example: https://github.com/galex/android-mapviewballoons My problem is that the clickable area is wider than the marker itself. For example, my Google Map marker is 25x25 then the clickable area would extend up to 70x70. This is a bi

  • Using the google maps api for reverse geocoding lat/long from iPhone 2009-05-27

    I am currently using the google maps' reverse geocoding API to convert long/lat received from an iPhone's CoreLocation API into city/state information on a google app engine server. Would this be considered a violation of the terms? I've done some re

  • Creating custom HTML info windows for markers in using the Google Maps API 2010-02-28

    I'm creating an array of information to populate markers using the Google Maps API. How can I use the following script to add html to the markers? The array while be built in a php loop. var markers = []; var title = []; var point = new GLatLng(51.50

  • Compiling zeroMQ (0MQ) for use on the iPhone 2011-02-13

    I'm trying to compile the Objective-C bindings (https://github.com/jeremy-w/objc-zmq) for zeroMQ for use on the iPhone. I've added the "objc-zmq" project as a dependency to the project I want to include it in. Everything compiles, however, none of th

  • Using the Google-Maps-for-Rails Gem how would I find points within a specified distance to a given point? 2011-11-15

    I'm wondering if its possible to use the Google-Maps-for-Rails gem to find a list of points that are saved in the database that are within a specified radius of a given point. For example, how would I find all the points in my database that are withi

  • IOS/iPhone sample code for selecting a location via placename &/or map via use of Google Maps API? 2011-12-09

    Any IOS/iPhone sample code for selecting a location via placename &/or map via use of Google Maps API? So an example of letting the user select a location via user of both: - typing in location & letting Google Maps API attempt to find locati

  • How To Use Gmap (google Map) in C# 2008-12-11

    I'm wanting to use google maps and see a million ways to do it on the web. Some are javascript methods and some are asp.net server components with which I have hit and miss luck. What's the easiest and most reliable way using c# and asp.net? I have m

  • Is there a way to create custom pathways using the Google Maps API V3? 2010-04-27

    I need to create custom pathways using the Google Maps API V3 for a map of a campus I know I could create pre-determined paths based on checkpoints that I input, but I need to be able to make it so that a user using the map I've created can click fro

  • Using the Google map API in Android, using a proxy 2011-02-24

    I am using the google map API in a very basic app. But the problem is that on running the app, I only see the grid and the map controls...the map doesnt even load in the virtual device. I have internet running in the device...the browser seemed to be

  • Google Maps Marker Drag End Directions - Clear Previous Route 2011-12-05

    I am using the Google Maps API v3, and I have implemented a directions rendered based on a marker drag end function. But if you drag the marker again then the original set of directions are not removed. The map is generated based of a geocoding reque

  • using the google maps API - marker cluster 2012-02-17

    I really am a JS noob - I have never really used itbefore and am struggling using the marker clusterer google provide. I have rad the documentation here is the code <script src="http://localhost/wheredidmytweetgo/js/markercluster.js" type="text/ja

  • Using Custom Google Map in my Web App is legal or not? 2012-03-24

    I am developing a web application, i have following uses of google maps List some facilities like film theater in map as thumbnail with contact details (i have kept latitude and longitude in my database) show route and directions from user location t

  • Bing map conflicts when used with Google map API 2010-07-22

    I am using google map in my application. I have an application regarding to property sales and purchase for that I have used google map. But now I have to add Bird's Eye view in it. Since there is no such a concept in google map api, I have decided t

  • Is it possible to enable CTRL + Dragging for markers in Google Maps? 2010-09-28

    I'd like the user to only be able to drag a marker when the CTRL key is held down. Is this possible? --------------Solutions------------- Using CTRL, ALT, or SHIFT will probably be problematic, because these keys are used by the browsers to mean vari

  • Using google map in my website (https secured) shows error 2011-04-25

    My website uses https secure pages, but when I use Google map API to bring in a map inside my website, it shows error (like webpage contains insecure items).But if I removed this Gmap api the error is gone. Is there any way like I can use google map

  • How to show multiple "draggable directions" in a single google map 2011-07-07

    Hi I want to know how to add multiple draggable directions in a single google map. I mean I want to draw 2 or more paths between 2 points without changing the first one. I'm using JavaScript API3. Thanks.

  • how can I change marker options without using marker = new google.maps.Marker 2012-03-26

    I have found the function below which creates only one marker - which is what I want. But how do I change the marker options e.g. html - without creating a new one? i.e. the code below will move an existing marker using setPosition but what if I also

  • Build google map marker using encoding 64 image 2012-04-24

    I have the following fonction, that gets an image as parameter: function getIcon(img){ var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); v

  • Finding the great circle distance between three points using the Google Maps API v3 2013-06-17

    Unfortunately I was not able to find any code / example how to draw great circle distance *between three(3) points on google maps*?? Here is the javascript for two points. How can I make it work with three points? var geocoder; var map; var addresses

  • How to target google map marker using jquery 2013-07-09

    OK, so I'm simply trying to target a google map marker using jquery, so that I can add some effects and animation to it, but I'm not having much luck. I tried targeting the (custom) image I'm using for the marker like this: $("img[src*='icon_you']").

  • how to create a new joomla component for integrating the google map api 2010-06-04

    how to create a new joomla component for integrating the google map api --------------Solutions------------- Not sure what else you could need that wouldn't be covered by http://tech.reumer.net/google-maps/documentation-of-plugin-googlemap/welcome.ht

  • Creating a search form for generating a Google Map 2010-10-22

    I have to create a form that accepts as its relevant parameter the name of an Italian city. Once submitted, the form value should be sent to the Google Maps service which should in turn return a complete map showing the location of that city. I'm pre

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.075 (s). 11 q(s)