0

I have an application in angularjs, it runs with no errors but there's just one particular variable passed to the front-end that is not updating. I've declared, populated and initiated it in the controller, but it still sits at zero:

(function () {
 "use strict";
 angular.module("HtJobPortal").controller("JobController", JobController);

    JobController.$inject = ["$scope", "$rootScope"];

    function JobController($scope, $rootScope){      
        var job = this;
        job.global = $rootScope;
        job.pendingCount = 0;
        job.penStr = "http://...";

        job.getPending = function () {
            $.ajax({
                method: "GET",
                crossDomain: true,
                url: job.penStr,
                dataType: "json",
                success: function (data) {
                    job.fillPending(data);
                    job.pendingCount = data.Data.length;
                }
            });
        }

        // Initialise pending and set roles
        job.init = function () {
            job.getPending();
        }
    };
    job.init();
    }
})();

The rest of the application works as expected and the job.fillPending(data); works with no errors and creates the table it's supposed to. I've put a break point on job.pendingCount = data.Data.length; and that tells me that the length is 1, but this is never reflected in the pending count, which remains at zero.

The HTML output is:

<h3>Showing {{job.pendingCount}} Pending Bookings</h3>

This does change if the initial creation variable at the start of the controller is changed. (EDIT: The reason 'job' proceeds the count, is because the controller has an alias when attached to the template <main class="main" ng-controller="JobController as job">)

4
  • Could you provide the html? My suspicion is that it's not properly bound, in the html {{}} refers to items on $scope and $scope !== this Commented Jul 3, 2017 at 15:26
  • 2
    use $http service instead of $.ajax and recommend to do read up on Thinking in AngularJS” if I have a jQuery background? Commented Jul 3, 2017 at 15:27
  • 1
    Just added the HTML - forgot to add it to the question Commented Jul 3, 2017 at 15:27
  • Possible duplicate of 'this' vs $scope in AngularJS controllers Commented Jul 3, 2017 at 15:28

1 Answer 1

1

Event handlers are called "outside" Angular, so even if your $scope properties will be changed, the view part will not be updated because Angular doesn't know about these changes.

Call $scope.$apply() after your event handler. This will cause a digest cycle to run, and Angular will notice the changes you made to the $scope and update the view.

Sign up to request clarification or add additional context in comments.

8 Comments

Adding that call to the end of the init (so after the getPending call) stops it from displaying completely
Alternatively $http is an option (it's also recommended)
While technically accurate, it misses an important opportunity: to teach. Your answer fails to discuss the "Angular Way" of solving this, which is to not use jQuery, but instead use the built-in Angular tools ($http).
Using the $http call means that I'll lose my cross domain attribute
Taking $scope.$apply() out of the init and placing it directly after the assignment fixed the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.