Angular not detecting changes in string variables

Issue with variable string not updating

var angulargap = angular.module("angulargap", []);
angulargap.factory('cartService', function($rootScope,$http){    
    var fac ={
        message:"factory",
        getCart:function(call){
            $http.post("/rpc.php", {app:"get_cart",pag:"cart"})
            .success(function(data, status, headers, config) {
                fac.setMessage("success");      
            }).error(function(data, status, headers, config) {

            });

        },
        setMessage:function(m){fac.message=m;} 
    }
    return fac; 
});
angulargap.controller("iridium", function ($scope,cartService){     
    cartService.getCart();  
    $scope.message=cartService.message;
});

Instead of updating an array, everything works fine. What could be the issue here? Thanks

Answer №1

switch out this line

 setMessage:function(m){fac.message=m;} 

to be

 setMessage:function(m){message=m;} 

Answer №2

My problem was solved by replacing the string variable with an object

var angulargap = angular.module("angulargap", []);
angulargap.factory('cartService', function($rootScope,$http){    
    var fac ={
        messages:{cartnum:"0"},
        getCart:function(call){
            $http.post("/rpc.php", {app:"get_cart",pag:"cart"})
            .success(function(data, status, headers, config) {
                fac.setCounter("num from server");      
            }).error(function(data, status, headers, config) {

            });

        },
        setCounter:function(m){fac.messages.cartnum=m;} 
    }
    return fac; 
});
angulargap.controller("iridium", function ($scope,cartService){     
    cartService.getCart();  
    $scope.messages=cartService.messages;
});

Now everything is functioning correctly and I can see the results on the frontend. Can anyone clarify why using a string variable did not work?

This is how I'm currently displaying it:

{{messages.cartnum}}

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

Tips for differentiating between elements with identical values in an HTML datalist using Angular

My boss is insisting that I use a datalist in our website interface to select an employee, even though there's no way to determine if the user typed in the name or picked from the list. The challenge is that the list must only display full names, but ...

Run several asynchronous HTTP requests in the background within a Chrome extension

I am facing a situation where I need to make multiple ajax requests through a Chrome extension and display the successful results in the popup HTML of the extension. I have created an array of URLs and loop through them to perform the ajax requests. Every ...

Utilizing Grunt to streamline a personalized project

I have set up Grunt as my "JS Task Runner". Node.js is installed in the directory "C:/Program Files". NPM has been installed in C:/users/Peterson/appdata/roaming/npm. Inside the npm folder, I have Grunt, Bower, and Grunt-cli. I am working on a pro ...

Identifying a particular pattern in a JavaScript string

What is the best way to check if a JavaScript String includes the pattern: "@aRandomString.temp" I need to verify if the String includes an @ character followed by any string and finally ".temp". Thank you ...

Displaying Bootstrap star rating twice in a recursive manner

After reading about the issue with Bootstrap star rating displaying twice on Stack Overflow, I found myself facing a similar problem. <div class="row"> <input id="input-id123" type="number" class="" min=0 max=6 data-stars=6 step=1> </di ...

Angular JS: Converting a Number to Words is not functioning correctly

The main objective is to convert a number input into words. Currently, it only displays the number in the text box without converting it. HTML: <div ng-app="app" ng-controller="myCtrl"> <span ng-init="W_cst='<?php echo $Cex; ?>&apo ...

What is the method for including a placeholder with sequential numbering?

When I click on the "Add String" button, it clones the first table row with an input in the table and adds it to the table. I also need to add a +1 number in the placeholder of the copied element. How can I determine the last placeholder before copying and ...

Determine the moment at which the input is altered by adjusting the slider

I'm encountering an issue with making this work. My goal is to calculate input bmi_val whenever one of the other 2 inputs is modified. These inputs can be changed either directly by the user (entering a value into one of them) or through a jQuery sli ...

Showing the image as a backdrop while scrolling through text

How can I create an effect that continuously displays the image while scrolling text in Internet Explorer without using position: sticky or position: fixed? var sticky = document.querySelector('.sticky-container'); var img = document.querySele ...

Explore flat data querying using Firebase and AngularFire

I am working with data stored in firebase, utilizing a flat structure with key-value pairs to establish a many-to-many relationship between events and users (see figure 1). While it's straightforward to look up events associated with a user using pure ...

ReactJS component disappearing behind the Navbar

When using my React app, I have a navigation bar at the top. The Navbar component is called in App.js, and the code snippet below shows how it is implemented. export default function App() { return ( <Router> <Fragment> ...

Leveraging the power of ajax to securely save information in a database with the web2py framework

Struggling with a major issue here. I have set up the following tables db.define_table('post', Field('user_email', default=auth.user.email if auth.user_id else None), Field('title', 'strin ...

The synergy of Redux with scheduled tasks

In order to demonstrate the scenario, I have implemented a use-case using a </video> tag that triggers an action every ~250ms as the playhead moves. Despite not being well-versed in Flux/Redux, I am encountering some challenges: Is this method cons ...

The Power of Json, Ajax, and Javascript

Is there a way to regularly check for updates and update the relevant cell accordingly? I want the updated cell to flash and change color to red/green based on if it is a negative or positive numeric value. I will be using JQuery, Ajax, and JSON... Best ...

Is there a way to recognize AJAX requests within Python's Flask framework?

Is there a way to identify if the browser sent an AJAX request through AngularJS, allowing me to respond with a JSON array? Alternatively, if not an AJAX request, I need to know how to render the template. Any suggestions? ...

Tips for maintaining a user's session post-login with Passport and Express JS

I recently set up a node backend using express and integrated Passport for authentication purposes. My application has a route called /login for logging in and another route called /me to retrieve information about the currently logged in user. Below is t ...

Is it possible to run my NPM CLI package on CMD without needing to install it globally beforehand?

I've created a new NPM package, complete with its own set of CLI commands. Let's call this package xyz, and let's imagine it's now live on npmjs.com Now, picture a user who installs this package in their project by executing npm insta ...

"The latest version of Angular, version 15, experiencing issues with javascript loading

Currently, I am diving into the world of Angular and encountering a puzzling dilemma. Surprisingly, my application performs flawlessly on various browsers such as Chrome, Firefox, Brave, Opera, and even on mobile versions except for Safari. Both the deskto ...

The navigation bar in React Router is interfering with the loading of other components

Currently, I am in the process of setting up a simple navigation bar that consists of basic buttons without any complex functionality. However, I have encountered an issue where placing the navbar inside the switch prevents other components from loading ...

Angular's Restangular initiates a blank promise at the beginning

One of the services I created can fetch data from the server and cache it, or retrieve data directly from the cache. The code snippet below illustrates this functionality. While using .then statements in the client side for cached data, I've noticed ...