search

Home  >  Q&A  >  body text

angular.js - Problem with callbacks in angularjs controller

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?

巴扎黑巴扎黑2827 days ago568

reply all(5)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-15 17:02:45

    promise

    reply
    0
  • 为情所困

    为情所困2017-05-15 17:02:45

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 17:02:45

    To put it simply, just write a service that encapsulates the code block, and use this service to initialize everything in the future

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:02:45

    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

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-15 17:02:45

    Use proxy mode to automatically fill in success. For specific implementation, please refer to $resource

    reply
    0
  • Cancelreply