角度渲染神秘的JSON数据

时间:2017-05-13 12:13:54

标签: angular angular-cli

我想渲染我的JSON,其中有一个嵌入隐藏对象名称的数据。

例如,在我的JSON中,我的第一个对象值具有键select player, stuff((SELECT distinct CHAR(13)+CHAR(10) + cast(score as varchar(10)) FROM yourtable t2 where t2.player = t1.player FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') from yourtable t1 group by player 。我使用for循环显示了值,但我也希望得到这个神秘的对象值并将其作为参数插入到UI中的-Kk0GA4lzQ5Ze8TEaYfS函数

deleteData
 <div class="container-fluid">
   <div class="well" style="overflow:auto;">
     <button class="btn btn-primary pull-right" routerLink="/create">Create</button>
   </div>
   <table class="table table-bordered">
    <thead>
      <tr>
        <th>User Id</th>
        <th>Title</th>
        <th>Age</th>
        <th>Address</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let i of array; let a of data; let j = index" [id]="a">
        <td>{{i.firstName}}</td>
        <td>{{i.lastName}}</td>
        <td>{{i.age}}</td>
         <td>{{i.address}}</td>
        <td><button class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span></button></td>
        <td><button class="btn btn-primary" (click)="deleteData(j)"><span class="glyphicon glyphicon-remove"></span></button></td>
      </tr>
    </tbody>
  </table>
 </div>
data:any;
dataLen: number;
array: any[] = [];
keyArray: any[] =[];
  constructor(private dataService:DataService) { }

  ngOnInit() {
    return this.dataService.getData()
      .subscribe((data:any) => {
          const myArray = [];
          for(let key in data){
              myArray.push(data[key]);
          }
          this.array = myArray;
    });
  }

1 个答案:

答案 0 :(得分:0)

如果你不介意,一个简单的方法就是把那个神秘的密钥作为你循环中对象的一个​​属性:

.subscribe((data:any) => {
   const myArray = [];
   for(let key in data){
     let a = data[key];
     a.key = key;
     myArray.push(a);
   }
   this.array = myArray;
});

然后在您的删除功能中,您可以传递i.key,因为它将是对象的属性:

(click)="deleteData(j, i.key)"

希望这适用于您的用例:)