Exploring AngularJS Scope Integration with Google Maps

Struggling with modifying an Angular Model from a GMaps event, here is the code snippet causing the issue:

function CtrlGMap($scope) {

var mapOptions = {
    center: new google.maps.LatLng(-54.798112, -68.303375),
    zoom: 11,
    //disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.SATELLITE
};

map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);

google.maps.event.addListener(map, "click", function(e) {
    lat = e.latLng.lat();
    lng = e.latLng.lng();

    $scope.lat = lat;
)};

HTML:

<body ng-controller="CtrlGMap">

<div id="mapCanvas"></div>

<form role="form" style="width: 30%; margin:0 auto;">

    <div class="form-inline">
        <div class="form-group">
            <label class="sr-only" for="lat">Latitude</label>
            <div class="input-group">
                <span class="input-group-addon">Lat</span>
                <input type="text" class="form-control" id="lat" ng-model="lat" disabled>
            </div>
        </div>
    </div>
</form>

Curious why there's no access to $scope within the GMap event. Any ideas?

Answer №1

When making changes to the scope outside of AngularJS, it is important to remember to call $scope.apply().

Here's an example:

google.maps.event.addListener(map, "click", function(e) {
    lat = e.latLng.lat();
    lng = e.latLng.lng();

    $scope.lat = lat;
    // Notify AngularJS
    $scope.$apply();
)};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding a space after a comma automatically upon saving changes in VSCode

Whenever I input code (t,s) into the system and make changes to it, it automatically transforms into (t, s) with an added space after the comma. Is there a way to avoid VScode from adding this extra space on its own? ...

Angular4 is throwing an error stating that the center element in the HTML is an undefined function

Is there an alternative tag that can be used instead of <center> <\center> in angular4, as it indicates that center is not a recognized function? ...

My pathways are clearly mapped out, yet express is returning a frustrating 404 error

After exhausting all the similar posts on StackOverflow without finding a solution... Let's take a look at my app.js, which is the first file that the express library seeks when launching the app right after my server.js file responsible for setting ...

How can I determine the Windows service pack version directly from my web browser?

Is there a method to determine the installed service pack from the browser? It doesn't seem to be available in System.Web.HttpBrowserCapabilities in asp.net. I am looking for a solution to alert users about the necessity of updating to XP Service Pack ...

What is the process to change the jQuery quiz outcome into a PDF when a user clicks a

I'm currently working on a jQuery quiz project where I want users to be able to download their results in PDF format with just one click. After doing some research, I came across the dompdf library, which allows me to convert HTML to PDF. If you&apos ...

What is the process of converting an arrow function into an asynchronous one?

In the code snippet below, I am checking for the existence of a file and if it is present, I am parsing the JSON data from it: fs.access(dbPath, (err) => { if (err) throw err else{ console.log('Database found. Processin ...

An AngularJS-powered dynamic navbar

Apologies if my explanation is unclear, but I am struggling to find a simple way to convey the following information. I have set up a navigation bar that displays different categories of articles. These navigation items are pulled from a database and can ...

Using jTemplates' $P parameters in a {#if} statement

Can the jTemplates' $P.imagesPerRow parameter be utilized within the {#if} condition? I am encountering an "Uncaught 12" exception when I try to do so. {#foreach $T as record} {#if $T.record$index % {$P.imagesPerRow} == 0} </tr> ...

The JQuery function .html() fails to include all the HTML content retrieved from a JSON response

I've created some HTML using PHP's json_encode(), and it looks like this: ob_start(); if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); get_template_part( 'template-par ...

Unable to access, manipulate, and view a font file in nodejs

Issue : I encountered a discrepancy when downloading a file using the request module and writing it to the file system compared to directly downloading it using a browser. The file in question is a font located at https://assets.ajio.com/static/assets/ec ...

Encountering the error message "npm ERR! code ENOVERSIONS" while trying to create a new

Issues persist when launching a new Angular project. npm ERR! code ENOVERSIONS npm ERR! No valid versions available for timed-out For example:- ng new training-app installing ng2 create .editorconfig create README.md create ...

What steps are required to send a request to the Discord bot API?

What is the correct way to send a discord API request, and where can I obtain a valid token for this purpose since my discord bot token did not work in that scenario? async function APIrequest(){ const lib = require('lib')({token: ' ...

The Ray Intersect function in THREE.js encounters issues when a div element is introduced

I have encountered an issue with my Three.js script. It works perfectly fine when there is only one target div on the page holding renderer.domElement. However, when I add another div with fixed height and width above the target div, the ray.intersectObjec ...

The left and right arrows maintain their visibility even when they are outside of the image

I am a beginner in the world of web development and I am trying to implement left and right navigation for images using arrows. My goal is to have the arrows fade in when the mouse hovers over the image and fade out when it's moved away. However, I am ...

ReactJS error handling when API limit is exceeded

Just starting out with ReactJs and trying to fetch data from the TruePush API. Here's the code snippet: export const getAllCampaign = () => { return fetch(`https://api.truepush.com/api/v1/listCampaign/1`, { ...

Why is the code in jQuery being bypassed when the string contains '&'?

I'm currently working on setting up a mailto button that generates an email title and body. Everything functions as expected, but I noticed a glitch in the code. If the symbol '&' appears in the text, the remainder of the code doesn&apo ...

What are the circumstances in which callbacks will not be triggered in event listeners?

I recently wrote the following code snippet: <script> setTimeout(() => { document.body.innerHTML = "<div></div>"; let outerHost = document.querySelectorAll("div")[0]; outerHost.attachShadow({mode: "open"}); outerHost.shado ...

What are the steps to effectively utilize wdio.conf.js?

Currently, I am experimenting with using webdriverio in conjunction with the jasmine test framework. Running my tests by simply typing "jasmine" at the command line works fine for me. However, when I try to execute "wdio wdio.conf.js," it ends up opening m ...

The year 2016 marked a significant advancement in transmitting HTML data to servers

I'm currently extracting data from web pages (similar to a situation with instapaper) and transferring it back to a LOCAL server, not over the internet. Both will be on the same machine, but I want to enhance security measures. Right now, I am retrie ...

The issue persists with `getServerSideProps` as it fails to retrieve data even when executed within the

Hey there! I'm currently facing an issue with fetching data in my Next.js app using getServerSideProps. The data is not being retrieved as expected, and I'm getting either an empty object or undefined in the console. I've tried various Next. ...