简体   繁体   中英

Angularjs how to share data between two directive via $http

I have two directives.There is a asynchronized function in link in each directive like $http or ngResource . I want to click button A to get data from backend via directive A, then change the data in directive B and its view.

I try to use a data service with $boastcast as most people do to share data between directive A and B, and use $apply to update the view. But I frequently get the inprog error. So what is the best way to solve it out?

That error ocuur when you are runnig $scope.$apply . When you try to manually run the digest loop that is alread runing then this exception will occur

So check if digest is running before use apply

if(!$scope.$$phase) { $scope.$apply() }

Instead of wrapping code with $apply, try to inject $timeout and wrap your code with $timeout. It will run digest cycle im safe way, only when cycle is not running already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM