0

Imagine we have an external module has a function that needs a callback parameter for a async task. As our parameter is a function, and they call that callback function in their module, so how do we use $scope that is out of our digest cycle? How angularjs handles such things?

And also we use $scope.apply() or $scope.digest() in such situations, so how do they work?

Note: I don't need you to provide some codes necessarily, just want to know about concepts. Thanks.

1 Answer 1

1
externalModule.doSomething(param, function(result) {
  $scope.result = result;
  $scope.$apply();
})

That's it actually. However it is good style to wrap all external modules - as an example you can look at $http, $interval, $timeout wraping calls to javascript functions. So you should put this code in some factory externalModuleWrap:

module.factory('externalModuleWrap', function($rootScope, $q) {
  return {
    doSomething: function(param) {
      var defer = $q.defer();
      externalModule.doSomething(param, function(result) { defer.resolve(result); }
      $rootScope.$apply();
      return defer.promise;
    }
  }
})

Now you can call externalModuleWrap from any point of your angular project, without problems.

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

Comments

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.