0

The UI binds but with {{total}} showing as -1 I do see the console log 'data came back'. Why dont I see 99 ?

myModule.controller('dashboardCtrl', ['$scope', '$http'
    , function ($scope, $http) {
        $scope.total=-1;
        $http.get('api/controlPanel/dashboard').success(function (data) {
            $scope = data.stats;
            $scope.total=99; // just to make sure the value changes
            console.log('data came back');
        });
    }
]);

I read a lot of blogs talking about $scope.$apply yet it is undefined. Any help would be appreciated

3
  • 2
    what does it read if you comment out '$scope = data.stats;'? Commented Mar 8, 2015 at 9:49
  • 2
    Please don't assign data.stats to your $scope.. Commented Mar 8, 2015 at 10:04
  • It is because of the asynchronous operation of the javascript. It executes your $scope.total and send a request to the server. Your html binds the -1 value. You need to delete the $scope.total = -1 and best practise is to use resolve method before you load the controller. Commented Mar 8, 2015 at 10:14

1 Answer 1

2

My first inclination is that you are redefining the $scope object and so the bindings are not taking effect. I would try $scope.stats = data.stats instead to avoid this problem.

This might be good to read: https://docs.angularjs.org/guide/scope

Hope this helps!

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

1 Comment

100% right. $scope carries a lot more than just data. By overriding it with my object I pretty much killed it.

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.