从服务器 json 的角度加载数据


Angular loading data from server json

有人可以告诉我在ng repeat中使用jsonObjects的最佳解决方案是什么?我的代码如下:

我的PHP回应是这样的:

die(json_encode(array('sts'=>'success', 'title'=>'*****', 'msg'=>'*******', 'data'=>$result)));

我的角度.js服务是这样的:

LeadApp.service('LeadsService', function ($http) {
  var AllLeads = {
    async: function() {
        var promise = $http({
            url: '../app/ServerData.php',
            method: "POST",
            params: { req_id: 'leads' }
        }).then(function (response) {
        return response.data;
      });
      return promise;
    }
  };
  return AllLeads;
});

然后在我的控制器中,我调用服务并设置一个潜在客户变量以在我的视图中使用 ng repeat:我的数据正在加载,我已经保证了。我从下面附上了控制台日志的图片。但不知何故,ng 重复不会像预期的那样工作......我做错了什么?没有错误!

    ....
        LeadsService.async().then(function (d) {
                this.leads = d.data;
                console.log(this.leads);
       this.list_all = this.leads;

这是视图中主要的ng重复部分(自定义ng重复与"dir-paginate"):...

<div class="widget-content leads_list" ng-controller="leadsController as leads">
<tr dir-paginate="lead in leads.list_all | orderBy:sortKey:reverse | filter:search | itemsPerPage:15" pagination-id="leads.list_all">
    <td scope="row">{{lead.id}}</td>

您需要在

方法之外绑定this上下文then

由于您使用的是 ng-controller="leadsController as leads" ,因此最好绑定到变量 leads

var leads = this;
LeadsService.async().then(function (d) {
        leads.list_all = d.data;
        console.log(leads.list_all);
});

then方法内部的this上下文与then方法外部的this上下文不同。此外,绑定到模板中使用的相同名称还有助于避免混淆。

this不是该回调函数上下文中的控制器。因此,您需要将this分配给控制器中的变量。

 var ctrl = this;
 LeadsService.async().then(function (d) {
        ctrl.leads = d.data;
        console.log(ctrl.leads);