AngularJS version 1.3 faces issues with updating bindings when a controller without $scope retrieves data from a webapi

After checking out the link provided below, I noticed that in Angular 1.3 they advise against using '$scope.x' inside the controller and instead suggest using 'this.x'

https://docs.angularjs.org/api/ng/directive/ngController

The issue seems to arise when calling $http to retrieve data (which is inherently asynchronous) as the binding doesn't update when a webapi call takes 2.4 seconds.

Even though the webapi call is made and returns data, the information doesn't reflect on the page.

Most examples I've seen utilize the $scope usage pattern. I'm aiming to future-proof my code whenever possible.

Here's the JSON data returned from the service:

[{"Venue":"Index","Symbol":"ADDA.IDX","SecurityId":3320,"Type":"Index","Description":"AMEX Advance Decline Difference"},

{"Venue":"Index","Symbol":"ADDD.IDX","SecurityId":3321,"Type":"Index","Description":"OTC Advance Decline Difference"},
...

Below is the markup:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<body>
    <h1>My Test</h1>
    <div id="t" ng-controller="SecuritiesController as venue">
        <ul>
             <li ng-repeat="s in venue.securities">
                 {{s.SecurityId}} {{s.Symbol}} {{s.Description}}
             </li>
        </ul>
    </div>
    <script src="scripts/angular-1.3.js"></script>
    <script src="app/controllers/securityController.js"></script>
</body>
</html>

And here's the controller:

angular.module('myApp', []).controller('SecuritiesController', function ($http) {
    $http.get("/api/securities").success(function(results)
        { this.securities = results.data; }
    );
});

Answer â„–1

Modify your code like this:

angular.module('myApp', []).controller('SecuritiesController', function ($http) {
    var controller = this;

    $http.get("/api/securities").success(function(results)
        { controller.securities = results.data; }
    );
});

It is important to note that within the response function, 'this' actually refers to the Closure block rather than your controller.

The original question author suggested removing the .data and it should work - a simple mistake!

Answer â„–2

Take a look at the updated code below.

angular.module('myApp', []).controller('SecuritiesController', function ($http) {
    var vm = this;

    $http.get("/api/securities").success(function (results)
    { vm.securities = results; }
    );
});

Please note that in Angular/javascript, it seems that 'this' is not supported in the closure, which is why we have to define a capture variable explicitly.

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

Removing the navigation button from the hamburger menu

I am working on creating a semi-progressive top navigation bar. For the mobile viewport, the navigation bar will only display the logo and a hamburger button. When the button is clicked, various navigation menu options as well as the Log In and Sign Up bu ...

Getting the Pesky JQuery Switcher to Actually Function

I am currently learning Jquery effects to enhance the interactivity of my websites. I have set up a practice page with the following structure: <article><a class="front-fire" href="1"></a></article> <article><a class="fron ...

How to loop through object properties in AngularJS using ng-repeat

I am working with an object structured like this: Obj = { "elements":[ {"name":"something","id":"v1-234"}, {"name":"somethingElse","id":"v1-239"} ], "paging":{ "next" : "100", "total": "2000" }, ...

Using node.js to set the billing address while confirming a Stripe Payment intent

Is there a way to specify the billing address of a payment intent with Node.js? When using the Stripe.js browser-side framework, I can easily accomplish this by utilizing Stripe elements and the stripe.confirmPayment function, which automatically captures ...

Issue: Unable to change onclick event using JavaScript I am facing a

Presently, I have this code snippet: function tp_visible(action){ if(action==1){ document.getElementById("tp").style.display='block'; document.getElementById("tp_action").onclick='tp_visible(0); return false;&apo ...

Tips for modifying the color or personalizing the header arrow for the expandableRow within Mui Datatable

My MUI data table has the expandable rows option, along with an ExpandAll button in the header row. The arrow displayed in the title row is confusing for users as it's similar to the arrows in all other rows. I want to change the color of the header a ...

Display the chosen alternative in a Bootstrap dropdown menu

I am currently facing an issue with the dropdown list in my bootstrap menu. <li class="dropdown"> <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="c ...

Attempting to execute Angular code within directives resulted in a syntax error during parsing

I am encountering an issue where the code provided below is causing an Error: [$parse:syntax] to appear in the console. <ul class="nav navbar-nav" ng-repeat="webpage in first.webpages"> <li><a href ng-click="tab = {{w ...

Avoid the expansion of line decorations in Monaco editor

I am looking to create a Monaco editor line decoration that remains contained within its original position when I press enter after the decoration. For instance, in a React environment, if I set up a Monaco editor and add a line decoration using the code ...

I am trying to display an element upon hovering using Jquery, but unfortunately, despite recognizing the event, it refuses to function as expected

I've been searching for a solution to show a submenu on hover by removing the hidden class that is hiding it. Despite trying various approaches, including using CSS and JavaScript, I haven't been able to make it work. Here is the code snippet I h ...

Having trouble resolving an error while attempting to incorporate an npm module into a vanilla JavaScript application

I admit my lack of expertise in building modern JavaScript apps is becoming evident. Currently, we have a Capacitor app that uses plain JavaScript without any build tools, and it functions well. Our goal is to incorporate Microsoft Code Push support throu ...

AngularJS Placeholder Translation

I have been using the Angular translator to translate my HTML buttons and labels. However, I have encountered an issue when trying to use it inside a placeholder for French language - junk characters appear. Oddly enough, the translator works perfectly fin ...

Having trouble with CORS errors persisting despite configuring CORS options for Google Authentication on React/Node/Passport

Currently, I'm in the process of developing a basic application using React for the frontend and Node/Express/MongoDB for the backend. User authentication is being handled through Passport, with local authentication and Google authentication both func ...

Personalize the appearance of your stackLabels in Highcharts with dynamic customization options

I recently created a bar graph using Highcharts. You can check it out here: http://jsfiddle.net/v1rbz41q/3/ Here's the code snippet I used: chartw.yAxis [0] .options.stackLabels.formatter = function () {              return "werfdc";   ...

Storing data with dots in MongoDB

I need help with saving JSON API data to a MongoDB collection. The JSON data I am working with has a structure like this: compatibility: { 2.7.1: { 2.2.6: [ 100, 1, 1 ] }, 2.8.3: { ...

Issue with Vue CLI 3: the hot reloading feature is causing significant delays in rebuilding the project

After updating a major project to utilize Vue CLI 3, I've noticed that the hot reloading process has become exceptionally slow: Upon opening a .vue file, it automatically rebuilds without any prompt, taking up to 10-15 seconds each time Whenever I s ...

Concealing columns in DataTables based on designated screen sizes

Issue I am facing a challenge with two DataTables — one with five columns and the other with four columns. My goal is to find a solution for hiding certain columns based on specific screen widths. Approaches I've Tested While resizing the browser ...

Looking to transform a response from a two dimensional array into a JSON object using Angular 9

I received the following response from my NodeJS backend: Current Response: [ [ '49' ], [ '33' ], [ '60' ], [ '58' ] ] I need to convert these values into JSON Object format as shown below in Angular 9 on my front ...

The command 'node' is not being recognized as either an internal or external command, potentially due to it being an operable program or batch file. This issue only arises when attempting to

Whenever I try to npm install a package or check the node/npm version, it works fine. However, upon attempting to start the app with any scripts, I encounter the following error message. [EDITED] $ npm start > <a href="/cdn-cgi/l/email-protection" ...