update the variables based on the changes in the service

I have developed a service in my application to retrieve configuration settings from the database. This service is used to display various configurations across different parts of the app. However, I am encountering an issue where the variables do not update on the configs page until I manually refresh the page.

THE SERVICE:

app.factory('dataShare', function($rootScope, $log, $q, $http, Data) {

  var configs= {};

      configs.getconfigs=function(){

        var deferred = $q.defer();

        Data.get('config').then(function(data){

          deferred.resolve(data.data);

        });

          return deferred.promise;

      }

    return configs;

});

THE CONTROLLER:

app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $log, $location, $http, dataShare) {
  dataShare.getconfigs().then(function(data){
    $scope.configs = data;
  });
});

HTML SNIPPET:

<div class="col-md-2"><span ng-if="configs[0].button_name"><a class="btn btn-sm btn-info navbar-btn" ng-href="{{configs[0].button_url}}">{{configs[0].button_name}}</a></span></div>

Any assistance or advice on this issue would be greatly appreciated.

Answer №1

To ensure updates on the "config" object, you can set a "Watch" on it within the configuration process. After initializing the "config" object, register it with the watch service in the following way: $scope.config={}; $scope.$watch('config', function() { alert('Config has been updated'); //Perform actions upon updating config }); //Service call

For more information, visit https://docs.angularjs.org/api/ng/type/$rootScope.Scope

UPDATE : Upon further review of the issue, if the problem lies in the view not reflecting the model's updates even after modification, utilize the $apply method:

dataShare.getconfigs().then(function(data){
    $scope.configs = data;
    $scope.$apply();
  });

This will prompt the angular view to show the recent changes in the model.

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

Guidelines for choosing and uploading a file using Ionic

Is there a way to upload a PDF file to a server using the $cordovaFileTransfer plugin? I currently have an input field like this: <input type="file" onchange="angular.element(this).scope().fileNameChanged(this)"> How can I retrieve the pathForFile ...

Ensuring that the text box only accepts the letters A, B, and C is essential for

Could you please help me with validating a text box? I would like to set it so that the user can only enter either A, B, or C. If they input D to Z or any other characters, I want a popup message to appear asking them to Enter A, B, or C. Would you recom ...

Is it possible for me to develop a mobile application using JavaScript, HTML, and CSS and distribute it for sale on the App Store, Google Play Store, and Microsoft Mobile App Store at

I have a keen interest in web standards such as Javascript, HTML, and CSS. My goal is to utilize these languages to develop applications for mobile devices like phones and tablets. I am also considering selling these applications on various platforms inclu ...

What is the process for altering a route in React 18?

Previously, I relied on the withRouter Higher Order Component (HOC) along with props.history.push() function to manage routes. However, with the introduction of React 18, these options are no longer available. My current task involves editing a post and ...

Send the form to the webpage and display various div elements based on the entered information

I have created a form that is designed to display one of four hidden divs on a page, depending on the input provided. Here is the form: <form> <input id="place" name="place" type="text"> <input name="datepicker" type="text" id="datepicker" ...

Navigate through stunning visuals using Bokeh Slider with Python callback functionality

After being inspired by this particular example from the Bokeh gallery, I decided to try implementing a slider to navigate through a vast amount of collected data, essentially creating a time-lapse of biological data. Instead of opting for a custom JavaS ...

Error: Attempting to update the value of 'ordersToDisplay' before it has been initialized in a re-render of React. This results in an Uncaught ReferenceError

Trying to dynamically update the document title to include the order number by clicking a button to display different numbers of orders from an array on the screen. The process involves importing a JSON file, filtering it based on user input, calculating ...

How is it possible that the SetTimeout code continues to run even after calling clearTimeout

I have this code snippet within my react component: //some code var timeout = null; //global variable //some more code useEffect(() => { if(...some condition) { console.log('test1'); timeout = setTimeout(() => { ...

Create a Vue application that is built on modules which could potentially be absent

I am currently developing a Vue+Vite app with module-based architecture. I have several Vue components and some parts of the app are functioning correctly. However, I want to ensure that the missing components are not imported or used in the application. H ...

Is there a way to utilize redux to trigger the opening of my modal when a button is clicked?

I'm facing a challenge with opening my modal using redux when clicking <CheckoutButton/>. Despite my efforts, the modal keeps appearing every time I reload the browser. I've reviewed my code but can't pinpoint the issue. What am I doin ...

The image will remain static and will not alternate between hidden and visible states

I am facing a challenge trying to toggle an image between 'hidden' and 'show' My approach is influenced by the post on How to create a hidden <img> in JavaScript? I have implemented two different buttons, one using html and the ...

Encountered CSRF validation error while working with a Python Django backend in conjunction with React frontend using Axios for making POST requests

I recently completed a tutorial at and now I'm attempting to add a POST functionality to it. Despite obtaining the csrf from cookies and including it in the "csrfmiddlewaretoken" variable alongside a test message in json format for the axios function ...

Is it wrong to use <match v-for='match in matches' v-bind:match='match'></match>? Am I allowed to incorporate the match variable from the v-for loop into the v-bind attribute on the

I am attempting to display HTML for each individual match within the matches array. However, I am uncertain if <match v-for='match in matches' v-bind:match='match'></match> is the correct syntax to achieve this. To clarify, ...

Selenium Python option other than using send_keys()

I was looking to enhance the efficiency of this Python code and found a faster alternative: driver.get(myUrl) message = driver.find_element_by_id('message') send = driver.find_element_by_id('submit') for _ in range(myRange): messa ...

Challenges arise with the sequential order of ngRoute code in Angular

When utilizing ngRoute to read URL parameters, I encountered a problem where I was unable to retrieve the ID while the code was running. However, when attempting to call it as a function, I was able to capture values from the URL. .controller('PageCt ...

Capturing and setting form element values within a Javascript Module Pattern

I'm looking to streamline my form element access using the Module pattern. The goal is to eliminate redundancy by organizing everything under a single module. How can I achieve this without having to directly call the Anonymous function within the mo ...

What is the best way to showcase the properties of multiple files with the help of

I have been attempting to utilize jQuery to exhibit the specifics of several uploaded files. Below is my code: <!DOCTYPE html> <html> <head> <title>jQuery Multi File Upload</title> </head> <body> <f ...

Utilizing UI-GRID to showcase JSON information

I am currently in the process of fetching data from the server. [ { id:1, name:demo, request: { id: 1, localCompany: { id: 1 } } }] [{ }, { }] This is how my JSON object appears to be structured. After calling ...

Enter a socket.IO chat room upon accessing an Express route

Encountering difficulty when attempting to connect to a socket.IO room while accessing a specific route in my Express application. The current setup is as follows: app.js var express = require('express'); var app = express(); var http = requir ...

What is the purpose of running "npm install" if the "node_modules" directory already exists?

When "npm install" is run on a project directory containing both a "package.json" file and a "node_modules" directory, what impact does it have? Does it replace the current modules with newer versions? Does it update them, or does it have no effect at all ...