首页 > 代码库 > AngularJS-webapp($q)

AngularJS-webapp($q)

$q延迟加载:

当一个数据需要请求多次,后面一个数据需要前面请求的数据时,我们就可以通过延迟加载进行数据传递

如下代码:

首先:我们需要得到职位信息:

{
  "id": "p3",
  "imageUrl": "/image/company-3.png",
  "name": "销售",
  "companyId": "c3",
  "companyName": "千度",
  "cityId": "c1",
  "cityName": "上海",
  "scaleId": "s3",
  "scaleName": "500人以上",
  "industryId": "i1",
  "industryName": "互联网",
  "salaryId": "s2",
  "salaryName": "3k-5k",
  "experience": "1-3年",
  "education": "专科",
  "benefit": "成长空间大",
  "description": "岗位职责:\n1.销售产品;..."
}

其次通过职位信息里面的companyId获得company信息:

{
    "id": "c1",
    "imageUrl": "/image/company-1.png",
    "name": "慕课网",
    "industry": "移动互联网",
    "state": "A轮",
    "people": "50-150人",
    "positionClass": [{
      "id": "jishu",
      "name": "技术",
      "positionList": [{
        "id": "p1",
        "name": "IOS前端工程师",
        "createdDate": "2016-04-16 23:30",
        "salary": "15k-25k"
      }]
    }, {
      "id": "yunying",
      "name": "运营",
      "positionList": [{
        "id": "y1",
        "name": "运营总监",
        "createdDate": "2016-04-10 13:30",
        "salary": "25k以上"
      }]
    }]
}

 

function getPosition() {
            var def = $q.defer();

            $http.get(‘data/position.json?id=‘ + $state.params.id)
                .then(function (response) {
                    $scope.position = response.data;
                    def.resolve(response.data);
                }).catch(function (err) {
                def.reject(err);
            });
            return def.promise;
        }

        function getCompany(id) {
            $http.get(‘data/company.json?id=‘ + id).then(function (response) {
                $scope.company = response.data;
            });
        }

        getPosition().then(function (response) {
           getCompany(response.companyId)
        });

 

AngularJS-webapp($q)