angular.js - Problem with callbacks in angularjs controller
巴扎黑2017-05-15 17:00:45
0
5
565
There are a lot of .success(function(data){}).error(function(err){}) in angularjs controller. Sometimes when binding $scope.model, you need to bind it in success. It’s so troublesome. Is there any better solution?
Have you written $http in the controller to request data? If so, it is recommended that you take out the part of requesting data, create a new service. js to request data, and then call the service in the controller. In this way, you only need to perform success and error once in the service, and only call it in the controller. Even if you call it repeatedly, there will not be so much redundant code.
promise
Have you written $http in the controller to request data? If so, it is recommended that you take out the part of requesting data, create a new service.
js to request data, and then call the service in the controller. In this way, you only need to perform success and error once in the service, and only call it in the controller. Even if you call it repeatedly, there will not be so much redundant code.
To put it simply, just write a service that encapsulates the code block, and use this service to initialize everything in the future
This is how I handle it. Write a baseService.js
Handle some callbacks in a unified way. For example, after an API callback, if an error is found, some problems need to be handled in a unified way
Then business-level apps have another Service.js. Mainly responsible for unified api management and providing data
Use proxy mode to automatically fill in success. For specific implementation, please refer to $resource