The data binding fails to update when new data is assigned to the variable

Seeking assistance as I am new to Angular and struggling with binding data. The issue I'm facing is that when I create a list using ng-repeat based on an array of items, later when I calculate the distance and duration of a direction and add this data to the array, it doesn't update the list unless I press an orderby button.

Any help on resolving this problem would be greatly appreciated.

Here is my app.js code snippet:


var app = angular.module("myapp", ['ui.bootstrap']);
var OpenWifiData = {}; //variable to store openWifiData
var initialLocation;
var geoLocation = new Boolean();
var distance = 999999999999999999999999999999999999;
var closest;
var OpenWifiDataReceivingStarted = false;
app.factory('myService', function ($http) {
    var myService = {
        async: function () {

            var promise = $http.get('http://localhost:3000/api/openData').then(function (response) {


                OpenWifiData = response.data;
                return response.data.data;
            });

            return promise;
        }
    }
    return myService;
});
app.factory("OpenDataService", function () {
    return { opendata: "" };
});

...

Below is the html code being utilized:

<div id="WifiSpots" ng-controller="ListController" class="col-sm-12 col-md-4 col-md-offset-2">
    <div  ng-repeat="item in openData | filter:searchText | orderBy: order">
        <ul class="list-group">
            <li class="list-group-item">
                <div class="row wifiSpots" >
                    <div class="col-md-2">{{item.locatie}}</div>
                    <div class="col-md-2">{{item.gemeente}}</div>
                    <div clas="col-md-1">{{item.distance}}</div>
                    <div clas="col-md-1">{{item.duration}}</div>
                    <button ng-controller="MapController" ng-click="calculateAndDisplayRoute(item.objectid)" class="btn ">Selecteer</button>
                </div>
            </li>
        </ul>
    </div>
</div>

Answer №1

The function navigator.geolocation.getCurrentPosition
is not part of Angular and does not automatically trigger the digest cycle. This means that changes to the UI will not be detected unless triggered manually.

To resolve this issue, you can use $apply to force the updates:

navigator.geolocation.getCurrentPosition(function(position) {

  $scope.$apply(function() {
    initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    geoLocation = true;
    map.setCenter(initialLocation);
    putMarkers(position.coords.latitude, position.coords.longitude, geoLocation);
  });

}, function() {

  $scope.$apply(function() {
    geoLocation = false;
    handleNoGeolocation(geoLocation);
  });

});

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

The Cross-Origin Request has been blocked: The Same Origin Policy prevents access to the remote resource located at

While attempting to use $http.get, I encountered an issue: Cross-Origin Request Blocked: The Same Origin Policy is preventing access to the remote resource at ..... This problem can be resolved by either relocating the resource to the same domain or enab ...

Trouble arises when utilizing a string method in JavaScript

I have some simple code, and yet am receiving an unexpected result. Let's take a look at the following function: const test2 = () => { let containsBitcoin = false const str2 = "buy bitcoin" if (str2.includes('bitcoin')) { ...

Creating a three-dimensional representation by projecting onto the surface of a sphere in ThreeJS

3D animation is a realm filled with numerous terms and concepts that are unfamiliar to me. I am particularly confused about the significance of "UV" in 3D rendering and the tools used for mapping pixels onto a mesh. I have an image captured by a 360-degre ...

AngularJS object array function parameter is not defined

Recently, I've been honing my AngularJS1x skills by following tutorials on Lynda and Udemy. One tutorial involved creating a multiple choice quiz. To test my understanding, I decided to modify the code and transform it into a fill-in-the-blank quiz. ...

Exploring how to browse and dynamically assign image sources with JavaScript/jQuery in an Asp.net mvc 4 framework

On my .cshtml page, I have an <img/> tag. <img id="ImageID" alt="" width="100%" height="100%" /> I want to dynamically set the src attribute by selecting the image file path using a file control. Although I attempted the following code in my ...

turning off next.js server side rendering in order to avoid potential window is undefined issues

I am currently managing a private NPM package that is utilized in my Next.js project, both of which are React and Typescript based. Recently, I integrated a graph feature into the NPM package and encountered an issue where any reference to window within t ...

Using React to pass a state value as a parameter in a method

Looking for a way to increase the reusability of my class. Dealing with a large form and don't want to create a unique method for each input. How can I pass the state value as a parameter to the method? I attempted the following approach: state = ...

Once this code is executed, Javascript ceases to function

I have developed a code snippet to create a typing effect similar to a command console. While the code is functioning well on its own, any additional code I add after it seems to malfunction. I've spent quite some time troubleshooting this issue witho ...

What is the best folder structure guideline for storing custom typings in a Vue project that utilizes TypeScript?

Where is the best place to store your custom type definition files in a Vue project? I typically use a directory called ./src/types along with this type of tsconfig: { "compilerOptions": { ... "typeRoots": [ "node_modules/@types", " ...

Installing libraries using npm can be done in two ways: using the command `npm install`

While working, we encountered an issue where the icon for the menu block from the rc-menu library was not displaying. Every time we run mvn install We also run npm install In our package.json file, we had included this library "rc-menu":"^5.1 ...

Using JavaScript to ensure that a background image is only displayed once in an HTML document

I am currently working with two cells, where I have set background images for these cells using Javascript from an array of images. The images available are: image1= 150*150 referred to as image1 once image2=150*150 referred to as image2 once However, ...

I'm experiencing difficulties with app.use() not functioning in my specific case, even though the paths are identical

My goal is to transfer the dist folder from the server to the client. When I use the following command, it works perfectly: app.use('/', express.static( Path.join(__dirname, '..', 'node_modules', 'del-js-webapp', &a ...

How to use jQuery to dynamically add a variable to a request in Laravel 5.2

Is it feasible to append a variable to the Laravel cookie in the client using jQuery? Understandably, the Cookie is linked to the request during GET? Can you include a variable in the $request container in Laravel 5.2 using jQuery before initiating a GET ...

Switching a material-ui input control instead of a textfield for materials-ui-datepicker: the ultimate guide

Within my React application (v16.3), I am utilizing the DatePicker component from the material-ui-pickers library to render date-picker controls. This particular component renders a Material-UI TextField. However, I am interested in modifying it to only di ...

Guide to shutting down Bootstrap 5 modal using React

Incorporating bootstrap 5 with react in my project without installation, I am utilizing CDN links to integrate bootstrap elements into the DOM. The challenge arises when attempting to close the bootstrap modal after data is updated. Despite trying to uti ...

Minimize/Maximize Swagger Response Model Class View

After successfully integrating Swagger API documentation with my rest services, I encountered a challenge. The Swagger page appears too lengthy due to the numerous response classes in my project, requiring users to scroll extensively to find information. ...

What is the process for exporting a plugin from dayjs() in JavaScript?

Currently, I have incorporated the plugin isToday() to enhance the capabilities of dayjs(). Nevertheless, I am uncertain about how to export isToday() in order to utilize it across other files. import isToday from "dayjs/plugin/isToday"; expor ...

Transferring user input to a child process in Node.js

I'm currently developing an online C++ compiler using the Node.js environment. With the help of the spawn function, I've managed to create a child process that compiles and executes the code, returning the output to the user. However, I'm f ...

Using JavaScript Object Constructor to alter text color

Seeking some guidance as a newbie here on how to deal with a problem I'm currently tackling. I understand that using an object constructor may not be the most efficient way to handle this, but I'm eager to enhance my knowledge of objects. My quer ...

Creating visually appealing Highcharts.js visualizations for both mobile and desktop devices

Has anyone had success implementing a responsive design with Highcharts to ensure charts look great on both mobile and desktop screens? By default, Highcharts do adjust when resizing the browser window, but sometimes the X-axis can become cluttered by tic ...