How to use the axios library with AngularJS

后端 未结 1 1611
名媛妹妹
名媛妹妹 2020-12-04 02:22

Why axios callback changes are displayed in angularjs, without using $apply

I was trying axios library on angularjs and I was surprised when I saw that the changes

相关标签:
1条回答
  • 2020-12-04 02:42

    How to use the axios library with AngularJS

    Bring its ES6 promises into the AngularJS context using $q.when:

      // axios example
      ̶a̶x̶i̶o̶s̶.̶g̶e̶t̶(̶u̶r̶l̶)̶.̶t̶h̶e̶n̶(̶(̶r̶e̶s̶p̶o̶n̶s̶e̶)̶ ̶=̶>̶ ̶{̶
      $q.when(axios.get(url)).then((response) => {
        $scope.axiosResult = response.data;
      });
    

    Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

    Also use the $timeout service instead of setTimeout.

      $timeout(() => {
        $scope.timeoutResult = {message: "timeout!"});
      }, 2000)
    

    The $timeout service is integrated with the AngularJS framework and its digest cycle.

    0 讨论(0)
提交回复
热议问题