代码之家  ›  专栏  ›  技术社区  ›  arunmmanoharan

函数内部的作用域不以html-angularjs格式打印[重复]

  •  0
  • arunmmanoharan  · 技术社区  · 6 年前

    为什么在angularjs中显示axios回调更改,而不使用$apply

    我在尝试 axios 图书馆在 angularjs 当我看到 $scope 在axios回调中被角度检测到。我想我得打电话 $apply 例如,当你使用 setTimeout .

      // axios example
      axios.get(url).then((response) => {
        // Here I don't need $apply, why??
        $scope.axiosResult = response.data;
      });
    
    
      // setTimeout example
      setTimeout(() => {
        // Here I need $apply for the timeoutResult to appear on the HTML
        $scope.$apply(() => $scope.timeoutResult = {message: "timeout!"});
      }, 2000)
    

    你知道为什么吗 申请美元 在Axios中不需要吗?

    编辑:

    A comment by georgeawg 帮助我发现我在用 $http 在另一个地方,我猜是由 $HTTP 也在帮助Axios回调被“消化”。

    0 回复  |  直到 6 年前
        1
  •  4
  •   georgeawg    7 年前

    如何使用 axios library 与安格拉

    使用 $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;
      });
    

    只有在angularjs执行上下文中应用的操作才能受益于angularjs数据绑定、异常处理、属性监视等。

    也使用 $timeout service 而不是 setTimeout .

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

    这个 $timeout 服务与angularjs框架及其摘要周期集成。