Issue with Angular variable not updating in the view, despite being successfully updated in the logs

This has been one of the most frustrating issues I've ever encountered.

Snippet from my html file:

    <!--test_management.html-->
    <div ng-controller="TestCtrl as tcl">
    <ul class="nav nav-tabs">
    <li class="active"><a href="#pane1" data-toggle="tab">Selected Tests</a></li>
    <li><a href="#pane2" ng-click="loadOnAdditionalData()" data-toggle="tab">Additional Testing</a></li>
    <li><a href="#pane6" ng-click="loadJenkinsData()" data-toggle="tab">Jenkins Jobs</a></li>
    </ul>
    <div class="tab-content" ng-controller="TestCtrl">
    <div id="pane1" class="tab-pane active">
    ... (remaining content omitted for brevity) ...
</div>

app.js (In TestCtrl controller.)

$scope.loadJenkinsData = function(){
  $scope.jenkins_jobs_data = [];
  $http.get("/jenkins_jobs_by_product/ESX").then(
    function(response){
  $scope.jenkins_jobs_data = response.data;
  console.log($scope.jenkins_jobs_data);
} );
};

When I open the tab and run loadJenkinsData, the console logs show that the data is retrieved correctly and stored in $scope.jenkins_jobs_data.

However, on the actual webpage, where the data should be displayed, it shows an empty array instead of the expected information.

I've used similar code in the past without issue. Why isn't it working now? Any tips on troubleshooting or fixing this problem would be appreciated.

P.S.: I attempted using $scope.$apply() at the end of loadJenkinsData(), but it didn't make a difference.

Answer №1

Check out this plunker for a live example. I've set up a dummy ESX file containing the string "some data in ESX".

It seems like there might be a scope problem, where $scope is different in the function(response) compared to loadJenkinsData. You can try:

$scope.loadJenkinsData = function(){
  var myscope = $scope;
  myscope.jenkins_jobs_data = [];
  $http.get("/jenkins_jobs_by_product/ESX").then(
    function(response){
  myscope.jenkins_jobs_data = response.data;
  console.log(myscope.jenkins_jobs_data);
} );
};

To prevent such issues, I recommend storing "this" in a variable like "vm":

var vm = this;
vm.loadJenkinsData = function(){
  vm.jenkins_jobs_data = [];
  $http.get("/jenkins_jobs_by_product/ESX").then(
    function(response){
  vm.jenkins_jobs_data = response.data;
  console.log(vm.jenkins_jobs_data);
} );
};

Give it a try and let me know if it works!

I've also updated the HTML to use the Controller as syntax:

<body ng-controller="MainCtrl as vm">
  <p>Hello {{vm.name}}!</p>
  <li><a href="#pane6" ng-click="vm.loadJenkinsData()" data-toggle="tab">Jenkins Jobs</a></li>
  <div id="pane6" class="tab-pane">
    {{vm.jenkins_jobs_data}}
  </div>
</body>

Answer №2

Experiment with Gregori's alternative code snippet and make adjustments to your display

<div ng-controller="TestCtrl as ctrl">
...
...
 <li><a href="#pane6" ng-click="ctrl.loadJenkinsData()" data-toggle="tab">Jenkins Jobs</a></li>
 ...
 ...
<div id="pane6" class="tab-pane">
  {{ctrl.jenkins_jobs_data}}
</div>
...

give it a shot :)

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

Change from using fs.writeFileSync to fs.writeFile

I have a question about changing fs.writeFileSync to fs.writeFile const users = { "(user id)": { "balance": 28, "lastClaim": 1612012406047, "lastWork": 1612013463181, "workersCount": 1, ...

The correct method for accessing descendants in THREE.js in the latest version, r68

As of the release r68, the getDescendants() method has been removed from the THREE.Object3D API. How should we now achieve the same functionality without any warning message being provided? ...

Creating images with LibCanvas

Currently, I am utilizing LibCanvas for canvas drawing on my HTML page. However, I am facing difficulty in drawing an image. Can someone provide assistance with this? UPDATE: The code snippet I am using is showing the image being drawn but then it disappe ...

Unable to execute specific php function using ajax

I have created a script that utilizes an ajax request. This script is triggered when a user clicks a button on the index.php page, like so: Risorse.php <form method='post'> Name <input type='text' name='nome&apo ...

How can I retrieve the value of a specific <span> element by referencing the class of another <span> within

I have come across the following HTML: <div class="calculator-section"> <p> <span class="x"></span> <span class="amount">30</span> </p> <p> <span class="y"></span ...

What is the best way to utilize an HTML form for updating a database entry using the "patch" method?

I have been attempting to update documents in my mongoDB database using JavaScript. I understand that forms typically only support post/get methods, which has limitations. Therefore, I am looking for an alternative method to successfully update the documen ...

angularjs ngmodel directive with dual values

I need help with customizing my dropdown control <div ng-repeat="prop in props" style="margin-bottom: 10px;"> <label class="col-md-4 control-label">Property {{$index + 1}} ({{prop.Value}})</label> <div class="col-md-8"> < ...

Does anyone else have trouble with the Smtp settings and connection on Servage.net? It's driving me crazy, I can't figure it out!

Whenever I attempt to connect to send a servage SMTP, it gives me this error message: SMTP connect() failed. I have tried using the following settings: include('res/mailer/class.phpmailer.php'); $mail->SMTPDebug = 2; include('res/mai ...

Node.js Express API returns an error status with an empty array response in a RestFul manner

Currently, I am in the process of developing a RestFull API using Node.JS to validate if a specific license plate is registered in my database (which happens to be MySQL). The endpoint that I have set up for this task is as follows: http://localhost:3009/_ ...

An abundant array of options spread across numerous dropdown menus

I have been tasked by a client to do something new that I haven't done before, so I am seeking the best approach. The project involves creating a form with three dropdown menus where users can select from thousands of options. For instance: Users mu ...

I encounter an issue with the ng-repeat in my code

It appears that the rendering is not working properly. http://plnkr.co/edit/IoymnpSUtsleH1pXgwFj app.controller('MainCtrl', function($scope) { $scope.lists = [ {"name": "apple"}, { "name": "banana"}, {"name" :"carrot"} ]; }); ...

Troubleshooting a malfunctioning custom controller in AngularJS

<html ng-app> <head> </head> <body data-ng-controller="Myfunc"> <ol type="i"> <li data-ng-repeat="c in cust | filter:name"> {{ c.name | lowercase}} - {{c.city | lowercase}}</li> </ol> ...

Exploring Angular 1.5 components: maximizing the potential of directives with ES6!

Within the directory labeled directives, I have created two files: directives.js and color.js I have imported directives into app.js Contents of directives.js: import angular from 'angular'; import ColorDirective from './color'; co ...

How can we determine in JavaScript whether a certain parameter constitutes a square number?

I've developed a function that can determine whether a given parameter is a square number or not. For more information on square numbers, check out this link: https://en.wikipedia.org/?title=Square_number If the input is a square number, it will ret ...

Error: Unable to access the 'receiver_id' property because it is undefined

This function is designed to notify the user when they receive a request in my messaging app for educational purposes. However, I am encountering an issue with the firebase console showing the error: firebase functions. TypeError: Cannot read property &ap ...

Maintaining the active status of section 1 after the page is refreshed using Javascript and CSS

I have a total of 3 buttons and 3 hidden sections available, which can be seen in the image below: click here for image description Whenever one of the buttons is clicked, the respective section transitions from being hidden to becoming visible: click he ...

Event in Bootstrap modal fails to activate

I've been attempting to link some code to execute when my modal is opened. Despite successfully opening the modal, I am puzzled as to why my event fails to trigger. Below is the structure of my modal: <div id="details" class="modal fade" style="d ...

Discover an Easy Way to Scroll to the Bottom of Modal Content with Bootstrap 5 on Your Razor Page

Currently, I am developing a web application that utilizes Razor Pages and Bootstrap 5 modals to showcase dynamic content. The challenge I am facing is ensuring that the content inside the modal automatically scrolls to the bottom when the modal opens or w ...

Accordion checkbox with dynamic features

Currently, I am dynamically populating data into a jQuery accordion. My goal is to include a checkbox just before the <h2> text. <div id="checkbox"> <h2> <span> <input type="checkbox" class="mycheck" value="apple" / ...

Selenium Refuses to Launch My Local Browser Despite Explicit Instructions

While using Chrome browser with selenium, I encountered an issue related to browser profiles. Everything works smoothly when the correct binary path is provided, but if an incorrect path is given, it refuses to run. The specific problem arises when the br ...