0

I am currently working on angular app and bumped into a problem. The problem is when I'm getting the data form backend through factory into controller, eventhrough I apply this data to scope I can't access it later in this controller.

My fist try:

Schema.list.get().$promise.then(function (data) {
    $scope.schema= data;
});

With this method I can print data in my view but if I when I want to access it in controller scope is undefined.

Second try:

Schema.list.get().$promise.then(function (data) {
    $scope.$apply(function() {
        $scope.schema= data;
    });
});

When I use $scope.$apply I got an error like this:

Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.6/$rootScope/inprog?p0=%24digest
    at angular.js:68
    at beginPhase (angular.js:16279)
    at Scope.$apply (angular.js:16020)
    at racunanje.client.controller.js:11
    at processQueue (angular.js:14678)
    at angular.js:14694
    at Scope.$eval (angular.js:15922)
    at Scope.$digest (angular.js:15733)
    at Scope.$apply (angular.js:16030)
    at done (angular.js:10545)

Thank you in advance for any help you can give me.

Miha2255
  • 61
  • 5
  • Could you describe how you try to access your data later in your controller? My guess is that you try in a synchronous way. – Adlen Afane Oct 24 '15 at 16:30
  • try defining `$scope.schema` in your controller when it's initialized and then update that value with the return of the promise – user2954587 Oct 24 '15 at 16:30
  • $scope.schema also if I console.log($scope.schema) result is undefined – Miha2255 Oct 24 '15 at 16:31
  • @user2954587 value is the same as predifined, it's not updated. – Miha2255 Oct 24 '15 at 16:34
  • 1
    what does `can't access it later` mean? Provide more code and explanation of what you are trying to do – charlietfl Oct 24 '15 at 16:40
  • @Miha2255 could you add the relevant code of your controller please? I guess that if you try to access it with a watcher you'll have the updated version but it may not be the best solution `$scope.$watch('schema', function () { console.log($scope.schema); })` – Adlen Afane Oct 24 '15 at 16:41

0 Answers0