lobengula3rd lobengula3rd - 5 months ago 7
Node.js Question

$scope inside a callback of a resource get call will not work

i am having a problem accessing the $scope. as it seems it is not working for me.

i have a resource:

squashApp.factory('CourtsService', function($resource) {
return $resource('/api/court/:num', {num: '@num'});
});


and my controller does:

squashApp.controller('CourtsController',
function CourtsController($scope, $window, $http, CourtsService) {
CourtsService.get({num:1}, function(data){
$scope.courts = data;
})

});


my server succeeds in returing a data which is in format of javascript object (JSON).
i have checked it. but for some reason the $scope is not updated and after this callback my view is not changing at all.

help please

Answer

I created a demo for you and your code looks ok.

However, it is always good to add some logic for the callback when the error occurs like this, so your app will not die silently.

squashApp.controller('CourtsController', function CourtsController($scope, $window, $http, CourtsService) {
    CourtsService.get({
        num: 1
    }, function (data) {
        console.log('success');
        $scope.courts = data;
    }, function (data) { // -> error handling
        console.log('failed');
        $scope.courts = data;
    })
});

Demo

Comments