Changing the variable within a promise in Angular 1

There seems to be an issue with updating a variable within the callback function of a promise, as shown in the code snippet below:

 $scope.showSelected= function (node){
                var promise = $http.get("http://127.0.0.1:5000/getResource?ldpr="+node.iri);
                promise.then(function(result){
                    node = result.data;
                });
        };

The $scope.showSelected function is a callback utilized by a widget. It takes a parameter node which needs to be updated within the promise's callback. How can I properly update this variable within the promise's callback function?

Answer №1

The $scope.showSelected function does not currently return a value. To retrieve a value from the asynchronous function call, you can use the .then() method to execute a task once the Promise returned by the asynchronous call is resolved.

 $scope.showSelected = function (node){
                         return $http.get("http://example.com/getData?param="+node.id);
                       };

 $scope.showSelected(node)
 .then(function(result) {
   // Do something with the result based on node
 })
 .catch(function(err) { // Handle any errors that occur
   console.log(err)
 })

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

How can I access the results of the hasMany relationship in AngularJS and Laravel?

Within the Laravel framework, I have successfully set up the API to display all products in a JSON format when accessing /products. Each product is associated with multiple stocks, indicating the quantity and size of the product available. For example, a p ...

Exploring different methods for drawing a polygon by iterating through JSON values

I am attempting to automatically draw a polygon using values stored in a database. The Python file I have returns JSON results to an AJAX call. In the AJAX success section, I need to iterate through the JSON data and automatically draw a polygon on a Googl ...

What could be the reason for the history.length being 2 on the initial page?

Why is it that when I open a new browser window and load a page in it, the history.length property is always 2 in both Chrome and Firefox? You can test this phenomenon yourself by visiting the following link: http://jsbin.com/amiyaw ...

Using a JavaScript class rather than an ID for toggling visibility

As a complete newbie to JavaScript, I've come across similar questions but I'm lost when it comes to coding - help needed! So here's the code I have so far, and I'm hoping someone can assist me. Currently, I have JavaScript set up to s ...

Utilize AJAX in JavaScript file

I am encountering an issue with the following code: function inicioConsultar(){ $(function(){ $('#serviciosU').change(function(){ if ($('#serviciosU').val()!= "-1") { $.ajax({ url: "@Url. ...

The equivalent of ESM for resolving modules using the `createRequire` function with a specified

In the process of developing a JavaScript instrumentation engine, I am currently focused on traversing a source file's Abstract Syntax Tree (AST) and queuing imported modules for instrumentation in a recursive manner. In order to achieve this, it is c ...

unable to press the electron button

I am currently working on a project that involves connecting PCs together for screencasting. While following an online coding tutorial, I encountered an issue with clicking the button to generate the ID code. Here is the code snippet from app.js: // Code ...

Setting up Npm Sequelize Package Installation

Could use some help with setting up Sequelize. Here's the current status: https://i.sstatic.net/MQH1I.jpg ...

Extracting necessary data from a JSON file and generating a new JSON file with the selected information

My task involves parsing a JSON file to extract events that fall within a specified start and end time provided via an HTTP GET request. The goal is to return these filtered events as a new JSON-encoded response. Despite brainstorming two potential solutio ...

JavaScript Flash player for iPad

As many of us know, the iPad does not support Flash. However, I am curious if the iPad sends any specific error messages that can be captured using Javascript. I realize one method is to detect the iPad from the user agent string, but I am interested in w ...

Issue in Angular Material: The export 'MaterialComponents' could not be located in './material/material.module'

I'm relatively new to Angular and I am encountering some difficulties when trying to export a material module. The error message that appears is as follows: (Failed to compile.) ./src/app/app.module.ts 17:12-30 "export 'MaterialComponents&ap ...

What are some methods to troubleshoot $injector type errors in TypeScript?

I'm currently facing an issue with my AngularJS code. Here is a snippet of what I have: this.$injector.get('$state').current.name !== 'login' But, it's giving me the following error message: error TS2339: Property 'c ...

React-redux: There is a TypeError in the code, as _this2.props.handleCityChange is not recognized as

I am currently working on integrating a weather widget in react-redux. I keep encountering the error message "TypeError: _this2.props.handleCityChange is not a function" and similar errors for other functions as well. Despite referring to the redux documen ...

Change every request's URL in $http through AngularJS globally

Let's consider a basic scenario: $scope.whatDoesTheFoxSay = function(){ $http.post("/backend/ancientMystery", { ... Is there a way to alter the URL for all post requests globally? I essentially want to add a prefix to every http request. I atte ...

Accessing multi-dimensional array properties in PHP with JavaScript integration

In my PHP code, I have an array structured like this: <?php $data = array(); $data[0] = array('year' => 2001, 'month' => array( 'January' => array('val1' => 1000, 'v ...

Click event in dropdown selection options

Is it possible to use ng-click in select options for different functions to be triggered on each option selection? Other threads have suggested using the same controller function for all options, but I am looking to trigger different functions based on t ...

Ways to compare two arrays based on a specific field using JavaScript

I have two arrays, known as array 'a' and array 'b'. var a = [ [1,'jake','abc',0 ], ['r', 'jenny','dbf',0] , ['r', 'white','dbf',0] ] var b = [ ['s&ap ...

"Integrating JavaScript in C# Code Behind: A Step-by-Step Guide

Is there a way to trigger this javascript function using C# code in the backend when the page loads? Your assistance is greatly appreciated, thank you. <script type="text/javascript"> document.onkeydown = function (event) { event = ( ...

Preview functionality is disabled in the iOS share extension

Currently, I'm developing a share extension for Safari on iOS. Our approach involves utilizing the default UI provided by iOS and extending the SLComposeServiceViewController class. In addition to this, I have incorporated a JavaScript function to ext ...

Is the row removed from the table after successful deletion?

I am struggling to remove the deleted row from the table. The code I tried is not working as expected. Here is the scenario: When a user clicks on the delete link/button, it sends a delete request and removes the data from the Database. After successful de ...