SoFunction
Updated on 2025-04-08

A brief discussion on the points of note about using $.ajax in angularJs

This article introduces the points to note about using $.ajax in angularJs. I will share it with you. The details are as follows

Technically speaking, it is not appropriate to mix angular and jquery, but why this topic is still debated in different ways?

In addition to convenience, it is also possible that some places of jquery are indeed more comprehensive than angular, such as ajax cross-domain.

When I usually use angular to develop, I basically just use angular. Even if there is this method, try to use as little as possible. I suggest that if you use a framework, try to use one, because these are encapsulation methods and there will inevitably be conflicts. It will be tricky if there is a bug.

Go straight to the topic:

When ajax cross-domain or parameter type (dataType) needs to be set, $http will appear very awkward, so at this time I thought of using $.ajax;

Originally, $.ajax is not as good as $http ink, but after writing it, I found that the problem came. The view layer completely ignored the object processed by $.ajax.

The feature of angular is two-way binding. To put it more complex, there is a dirty value detection system, mainly including: $watch and $digest;

There is a thing called $apply in triggering $digest. $apply is considered to be the most standard way to mix AngularJs with third-party libraries.

$.ajax({
  data:{},
  url:'',
  type/method:'',
  dataType:'',
  success:
    function(){
    $scope.$appy();
    }
  }
)

After the processing is completed, add $scope.$apply(). This method is also applicable to setTimeout, setInterval, etc....

But I still recommend not to use third-party libraries when you can.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.