angular框架的SmartAdmin模板 如何请求后台数据

公司新项目用angular 并且要求在smartadmin里写, 以前没用过,记一下。
1.在自己的模块里创建控制器controllers.js文件
文件里代码如下:

angular.module('app.Inspection').controller("inspection",
    function ($scope, Inspectionhttp ) {//Inspectionhttp是第二步里自己定义的名称,一定要传

        $scope.param = {//返回给后台的数据
            one: 1,
            two: 10,
            she: "",

        };

        $scope.loadData = function(pageNum) {

            console.log(pageNum);
            var param = {};
            angular.forEach($scope.param,function(item,val){//简单过滤空数据的函数
                if(item.toString()){
                    param[val] = item;
                }
            })
            param.pageNum = pageNum || param.pageNum;
            Inspectionhttp.getPurchaseData(param).then(function(res) {//调用Inspectionhttp里的方法,这个前面的名字一定记得根据自己写的名字改

                $scope.pageInfo = res //调取数据成功把数据赋给$scope.pageInfo
            }, function(err) {
                console.log('load');//失败在控制台打印load
            })

        }

        $scope.loadData();



    }
);

第2步。创建service.js文件 代码如下:

"use strict";

angular.module('app.integratedQuery').service("CurrentQhttp" //"currentQhtttp"是你定义的接口的名称

, function($http, $q, APP_CONFIG) {
    this.getPurchaseData = function(params) {

        var d = $q.defer();
        $http({
          method : 'POST',
          url : APP_CONFIG.baseUrl + '/storage/getTGStorages',
          params : params,
        }).then(function successCallback(response) {
          // 请求成功执行代码

          d.resolve(response.data);
        }, function errorCallback(response) {

          // 请求失败执行代码
          d.reject("error");
        });
        return d.promise;
      }
})

第3步。 HTML部分,因为我们公司这个项目大部分是表格,所以举个跟表格相关的例子:将获取到的后台数据渲染到table里

                        <table>
                                <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th data-class="expand">姓名</th>
                                        <th>性别</th>
                                    </tr>
                                </thead>
                                <tbody>  // pageInfo.list是第一步里存数据的
                                   <tr ng-repeat='item in pageInfo.list track by $index'>

                                        <td>{{$index}}</td>
                                        <td>{{itemName}}</td>
                                        <td>{{item.sex}}</td>

                                    </tr>
                                </tbody>
                            </table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值