码迷,mamicode.com
首页 > 移动开发 > 详细

AngularJS-webapp($q)

时间:2017-06-10 13:55:15      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:on()   ons   etc   span   传递   logs   需要   eject   param   

$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)

标签:on()   ons   etc   span   传递   logs   需要   eject   param   

原文地址:http://www.cnblogs.com/Nyan-Workflow-FC/p/6978413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!