简体   繁体   English

对象的角度回调问题

[英]Angular callback issue for objects

I have 2 API calls which provide 2 JSON Arrays. 我有2个API调用,它们提供2个JSON数组。 The controller is written as :- 控制器写为:

function RDetailController($routeParams, Rep) {
    var self = this;
    self.Name = $routeParams.Name;

    Rep.envGetSingle(self.Name).then(function (response) {
      // console.log(response.data.length);
      self.Details = response.data;
      console.log(self.Details);

    });

    Rep.repGetByCurDate(self.Name).then(function(response) {
      // console.log(response.data.length);
      self.report = response.data;
      console.log(self.report);
    });
  }

The template file uses the following Details as follows:- 模板文件使用以下详细信息,如下所示:

<div class="container">
<div>
<h1>Reports</h1>
<tr ng-repeat="a in $ctrl.Details">
  <th><h4>UI Url: <a>{{a.url}}</a></h4></th>
  <th><h4>Host IP: {{a.IP}}</h3></th>
  <th><h4>Release: {{a.mode}}</h3></th>
</tr>
</div>
<div>
<table class="table table-bordered table-hover data-filter-control="true" " >
  <thead>
    <tr>
      <th>#</th>
      <th>Screen ID</th>
      <th>Mast ID</th>
      <th>Description</th>
      <th data-filter-control="select">Result</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="result in $ctrl.report">
      <th></th>
      <th>{{result.ScreenID}}</th>
      <th><a href="#!/reports/{{result._id}}">{{result.MastID}}</a></th>
      <th>{{result.Description}}</th>
      <th><span ng-class="{
          'color-red': result.result === 'Failed',
          'color-green': result.result === 'Passed'}">{{result.result}}
  </span></th>
    </tr>
  </tbody>
 </table>
 </div>
 </div>

The Factory Rep is defined as :- 工厂代表定义为:-

factory('Rep', ['$http',
function($http) {
  return {
    repGetByCurDate : repGetByCurDate
    envGetSingle: envGetSingle
  };

  function envGetSingle(envName) {
    return $http.get('/api/envs/currDate/' + envName + '/envDetails').then(complete).catch(failed);
  }

  function repGetByCurDate(envName) {
    return $http.get('/api/envs/currDate/' + envName).then(complete).catch(failed);
  }

  function complete(response) {
    return response;
  }

  function failed(error) {
    console.log(error.statusText);
  }
}

]) ])

The factory returns the appropriate data. 工厂将返回适当的数据。 The problem happens when I try to access self.Details in the angular template file. 当我尝试访问角度模板文件中的self.Details时,会发生问题。 The self.report has values but self.Details is empty. self.report有值,但self.Details为空。 How can I persist object in self.Details as well? 我也该如何在self.Details中保留对象?

Turns out the Details variable is a single element in an array. 原来Details变量是数组中的单个元素。 So to access it we use a[0].url instead of a in the template file. 因此,要访问它,我们使用[0] .url而不是模板文件中的a。 This resolved the issue. 这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM