angularjs解决页面异步加载的问题

案列:由于项目中有地方一个API返回来了所有的信息,后面我需要根据这些信息进行筛选查询,就遇到了,前面的数据拿不到的               情况

解决方案:消息广播的机制

$scope.getColumnTree = function() {
      var path = "menu/getColumnTree.show";
      var param = {};
      param.menuId = $scope.menuId;
      // param.status = 1;
      videoService.serviceFunction(path, param, function(returnValue) {
        if (returnValue.status.returnCode != -1) {
          $scope.columnTree = returnValue.data.list;
          for (var i = 0; i < $scope.columnTree.length; i++) {
            // $scope.getCourseList($scope.columnTree[i].currentColumn.columnCode,i);
            $scope.classFlag = true;
            if ($scope.columnTree[i].childColumnSortTreeList == null) {
              $scope.columnTree[i].childColumnSortTreeList = new Array();
            }
            switch (i) {
              case 0:
                $scope.areaList = $scope.columnTree[0];
                break;
              case 1:
                $scope.artList = $scope.columnTree[1];
                break;
              case 2:
                $scope.schoolList = $scope.columnTree[2];
                break;
              case 3:
                $scope.cultureList = $scope.columnTree[3];
                break;
              case 4:
                $scope.scienceList = $scope.columnTree[4];
                break;
              case 5:
                $scope.lifeList = $scope.columnTree[5];
                break;
              case 6:
                $scope.appreciateList = $scope.columnTree[6];
                break;
            }
          }
          $scope.col_list = $scope.columnTree;
          console.log($scope.col_list);
          $scope.$broadcast("returnValueLoaded");
        }
      });
    };

这个时候我们需要用到广播$scope.$broadcast。发送广播以后,需要通过$scope.$on进行监听。

$scope.$on("returnValueLoaded",function(){
        $scope.getCourseList($scope.areaList.currentColumn.columnCode,0);
        $scope.getCourseList($scope.artList.currentColumn.columnCode,1);
    })

网上还有一些其他的推荐方法,推荐地址:https://blog.csdn.net/guduyishuai/article/details/52947723

你可能感兴趣的:(前端学习笔记)