猿问

angularjs是如何监听XMLHttpRequest事件的?

在线DEMO:

https://plnkr.co/edit/KAwhab7...

(function(angular) {

  'use strict';

angular.module('scopeExample', [])

  .controller('MyController', ['$scope','$timeout', function($scope,$timeout) {

   $scope.greeting='hhh'


   var httpRequest = new XMLHttpRequest();


    httpRequest.onreadystatechange = function(){ 

      console.log(httpRequest)

      if(httpRequest.readyState==2){ //不更新

        $scope.greeting='000'

      }

      // if(httpRequest.readyState==1){//更新 

      //   $scope.greeting='000'

      // }

    };


    httpRequest.onload = function(){

      // 不更新 

      // $scope.greeting='333'

    }

 

    httpRequest.open('GET', 'https://www.json-generator.com/api/json/get/bHEfYDDHEy?indent=2');


    httpRequest.send();


  }]);

})(window.angular);

为什么angularjs只有在readyState==1会更新数据?最好可以结合源代码解释


智慧大石
浏览 725回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答