1. 程式人生 > >angularjs解決頁面非同步載入的問題

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);
    })