来自服务器json的角度加载数据

时间:2022-06-18 09:48:04

Can someone tell me whats the best solution to use jsonObjects in ng repeat? My code is as follows:

有人能告诉我什么是在ng重复使用jsonObjects的最佳解决方案?我的代码如下:

My PHP response is this:

我的PHP响应如下:

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

My angular.js the service is like this:

我的angular.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;
});

Then in my controller i call the service and set a lead var to use in my view with ng repeat: My Data is being loaded i assured already. I attached a pic of the console log from below. But somehow the ng repeat just wont work like expected... What am i doing wrong? There is no error!

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

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

Here is the main ng repeat part in the view (custom ng repeat with "dir-paginate"): ...

以下是视图中的主要重复部分(使用“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>

...

...

2 个解决方案

#1


1  

You need to bind the this context outside then method.

您需要在方法之外绑定此上下文。

Since you are using ng-controller="leadsController as leads", it would be wise to bind to the variable leads.

由于您使用ng-controller =“leadsController作为线索”,因此绑定到变量线索是明智的。

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

The this context inside the then method is not the same as the this context outside the then method. Also binding to the same name that you use in your template helps to avoid confusion.

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

#2


1  

this is not the controller in that callback function context. So you need to assign this to a variable in the controller.

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

 var ctrl = this;

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

#1


1  

You need to bind the this context outside then method.

您需要在方法之外绑定此上下文。

Since you are using ng-controller="leadsController as leads", it would be wise to bind to the variable leads.

由于您使用ng-controller =“leadsController作为线索”,因此绑定到变量线索是明智的。

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

The this context inside the then method is not the same as the this context outside the then method. Also binding to the same name that you use in your template helps to avoid confusion.

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

#2


1  

this is not the controller in that callback function context. So you need to assign this to a variable in the controller.

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

 var ctrl = this;

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