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?

巴扎黑
巴扎黑

reply all(5)
phpcn_u1582

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

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template