Executing a function following another function in Angular: A step-by-step guide

Can you assist me with executing the GetAllSLUDetails() function first and then retrieving a broadcast value from the controllerTa?

//==============call the state function after changed Country Name // get broadcase value from the controllerTa=================    
$scope.$on('evtTACountryCodeSelect', function (event, args) {
    $scope.message = args.message;
    var taCountryCode = $scope.message.split(",")[0];
    var taStateCode = $scope.message.split(",")[1];

    alert(taCountryCode);
    alert(taStateCode);

    GetAllSLUDetails(taCountryCode);
    alert(taStateCode);

    if (taStateCode != "") {              
        document.getElementById("ddlState").value = taStateCode;
    }
});

//================To Get All Records ====================== 
function GetAllSLUDetails(CountryCode) {
   // alert('ctrl State' + CountryCode);
    var Data = stateService.getSLU(CountryCode);
    Data.then(function (d) {
        $scope.StateListUpdate = d.data;
        //alert(d.data);
        alert(JSON.stringify(d));
    }, function () {
        alert('Error');
    });
}

Answer №1

Could you please provide more detail about your intention? In general, you can achieve executing a function after another by utilizing callbacks or promises.

If you wish to perform an action after the GetAllSLUDetails function, you have two options:

$scope.$on('evtTACountryCodeSelect', function (event, args) {
    GetAllSLUDetails(taCountryCode, function() {  // Execute this function first
        // Carry out desired tasks
    });
});

function GetAllSLUDetails(CountryCode, callback) {
    // alert('ctrl State' + CountryCode);
    var Data = stateService.getSLU(CountryCode);
    Data.then(function (d) {
        $scope.StateListUpdate = d.data;
        //alert(d.data);
        alert(JSON.stringify(d));
        callback(); // Trigger the callback function here
    }, function () {
        alert('Error');
    });
}

Alternatively, you can make use of Promises:

$scope.$on('evtTACountryCodeSelect', function (event, args) {
    GetAllSLUDetails(taCountryCode).then(function() {  // Perform this function next
        // Do whatever needs to be done
    });
});

function GetAllSLUDetails(CountryCode) {
    return new Promise(function(resolve, reject) { // Create a new Promise object
        // alert('ctrl State' + CountryCode);
        var Data = stateService.getSLU(CountryCode);
        Data.then(function (d) {
            $scope.StateListUpdate = d.data;
            //alert(d.data);
            alert(JSON.stringify(d));
            resolve(d); // Resolve the Promise with data
        }, function (error) {
            alert('Error');
            reject(error); // Reject the Promise with an error
        });
    });
}

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

Is there an equivalent of getElementById for placeholder text?

I need help automating the input of information on a webpage using JavaScript. Each field has a unique ID, like this: <div id="cc-container" class="field has-float-label"> <input placeholder="ccnumber" id="credit_card_number" maxlength="16" ...

Utilizing AJAX to Invoke a Method in a CS Page: A Step-By-

I am trying to utilize AJAX to call a method in my CS page. Below is the design code I am using: <!-- Name --> <input type="text" name="name" id="name" required="required" class="form" placeholder="Name" /> <!-- Email --> <input type= ...

How to eliminate spacing between slides in a 3D Carousel Slider using Bootstrap 5

My website utilizes Bootstrap along with a carousel slider, which is functioning properly. However, I am encountering an issue when the slider transitions from right to left. It briefly displays a white space between slides, which I wish to eliminate. I wa ...

- "Queries about Javascript answered with a drop-down twist

Having some trouble with setting up a straightforward FAQ dropdown feature. Could someone lend a hand and see what might be going wrong? Appreciate your help! CSS #faqs h3 { cursor:pointer; } #faqs h3.active { color:#d74646; } #faqs div { height:0; o ...

Hierarchy-based dynamic breadcrumbs incorporating different sections of the webpage

Currently in the process of developing a dynamic breadcrumb plugin with either jQuery or Javascript, however I am struggling to implement functionality that allows it to change dynamically as the page is scrolled. The goal is to have a fixed header elemen ...

I am having trouble retrieving the specific ID value using jQuery from the table row, as it is displaying as 'undefined'

I am attempting to capture the value from a table row when the add to cart button is clicked. The table structure can be found below. <table class="table" id="myTable"> <thead class="thead-dark"> <tr> <th scope= ...

Masonry is experiencing compatibility issues with Bootstrap tabs

I have set up a bootstrap tab option and have also implemented masonry js. The issue I am facing is that the boxes appear differently in each tab; one appearing smaller and the other larger. The masonry layout seems to be working correctly in the first tab ...

Retrieving data stream from the redux store

My aim is to display a loading bar that reflects the progress of uploading a PSD file when a user initiates the upload process. For example: https://i.stack.imgur.com/fPKiT.gif I have set up an action to dispatch when the file begins uploading, and the ...

Implementing Alloy-Script/Javascript in dynamically loaded JSP files

I have been loading various JSPs dynamically using an Ajax call, but after the JSP is loaded, none of the JavaScript inside seems to be working. I suspect this is because the script has not been parsed yet. To address this issue, I came across the "aui-pa ...

Using Mongoose to assign a value in Node.js

I am facing an issue with my NodeJS application that communicates with an Angular app. Although this is a simplified version of the problem, I will do my best to explain it: When I receive a value from the socket connection, such as: { Id : "1", ...

What is the best way to retrieve data from within a for loop in javascript?

Seeking assistance in Typescript (javascript) to ensure that the code inside the for loop completes execution before returning I have a text box where users input strings, and I'm searching for numbers following '#'. I've created a fun ...

The function GetSomething() in sequelize.js is displaying inaccurate values when used in a hasOne relationship, but is showing the correct values in a BelongTo

I'm encountering an issue where trying to fetch a Fluxo through a relationship in my CanaisCadastro results in the GetFluxo() function returning the incorrect row. On the other hand, using the foreign key in a findOne query returns the expected value. ...

Using Javascript to link various arrays filled with file paths and transforming them into an object in a distinctive manner

I've tried countless times to make it work, but it just doesn't seem to work... I have an array that contains multiple other arrays: let arr = [ ["Users"], ["Users", "john"], ["Users", "john", "Desktop"], ["Users", "john", "Docum ...

Difficulty encountered while implementing the if-else statement in raycasting operations

Currently, I am experimenting with raycasting to determine if my mouse is pointing at an object. The function seems to be working fine when the object is not being touched - it correctly prints out "didnt touch". However, when the object is touched, it s ...

Exploring the Power of JQuery with Hover Effects and SlideToggle

I was struggling to keep the navbar displaying without it continuously toggling when my pointer hovered over it. I just wanted it to stay visible until I moved my pointer away. <script> $(document).ready(function(){ $(".menu-trigger").hover(funct ...

Matching stroke-dashoffset in SVG between two distinct paths

I am currently working on animating stroke-dashoffset for two different paths to create a drawing effect. There are buttons provided to adjust the stroke-dashoffset values. My goal is to ensure that the filled paths align vertically as they progress. Is t ...

The encoding of Node.js using npm

Looking to install the validate .json file below using npm: { "name": "node-todo", "version": "0.0.0", "description": "Simple todo application", "main": "server.js", "dependencies": { "express": "~3.4.4", "mongoose": "~ ...

Prevent the default scroll event triggered by the mousewheel in certain situations. It is not possible to use preventDefault within a passive event

I have a div element with the onWheel attribute. By default, the browser interprets onWheel as scroll behavior. However, I want to prevent the browser's default mouse behavior under certain conditions. Unfortunately, I encountered an error that say ...

Determine the selected item from a dropdown menu using JavaScript

Is there a way to retrieve the selected text from a dropdown menu in HTML? Here is an example of the code: <select id="playerType" onchange="copy();"> <option value="0">Select one</option> <option value="1">Goalkeepers</option&g ...

Embracing either dark or light mode

Can anyone give advice on how to integrate a feature like this onto my website? I've attempted to use plugins with no success. It doesn't need to be too complex. Does anyone have experience with this or know of a solution they could suggest? Alt ...