如何在角度中绑定对象中的嵌套列表

时间:2015-06-30 21:01:27

标签: javascript json angularjs

我有一个名为“posts”的列表,其中包含如下所示的对象。

{_id: "559301710de9c23407b237ee"
author: {user_id: "557c6922925a18a81930d2ce", username: "test"}
comment_id: []
content: "<p>123</p>"
created_at: "2015-06-30T20:52:01.738Z"
image_id: []
like_user_id: ["557c6922925a18a81930d2ce"]
likes: 1
tags: [{_id: "558ded1b8526b45407fd0bb1", tag_name: "1"}, {_id: "559301630de9c23407b237ec", tag_name: "2"},…]
title: "123"}

我想提取tag_name并在html

中显示它
<a ng-repeat="post in bests" ng-href="/post/{{post._id}}">
  <em class="tag">{{post.tags.tag_name}}</em> //This appearently doesn't work!
  <span>{{post.title}}</span>
  <em class="likes">+{{post.likes}}</em>
  <time>{{post.created_at}}</time>
</a>

除tag_name外,一切正常。

我尝试在ng-repeat中使用ng-repeat,它似乎不起作用。你是怎么做到的?

3 个答案:

答案 0 :(得分:3)

// without preallocation { int** y = new int*[n]; int* oldx=0; for (int i=0;i<n;i++) { int* x = new int[n]; // migrate old values in case you are modifying just a part of x if (oldx!=0) { memcpy(x,oldx,n*sizeof(int)); } //... do some stuff with x; y[i] = x; // keep reference to copy in next iteration oldx=x; } } // with preallocation { int ** y = new int*[n]; // preallocate memory for (int i=0;i<n;i++) { y[i] = new int[n]; } int* oldx=0; for (int i=0;i<n;i++) { int* x =y[i]; // migrate old values in case you are modifying just a part of x if (oldx!=0) { memcpy(x,oldx,n-sizeof(int)); } // ... do stuff with x // keep reference to copy in next iteration oldx = x; } } 位于数组中,因此您需要一个索引才能使用它

tags

或者,您可以使用{{post.tags[0].tag_name}} <em class="tag">{{post.tags[0].tag_name}}</em> //<== sample code that works | Your desired index goes here

进行迭代

答案 1 :(得分:3)

尝试

<em class="tag" ng-repeat="tag in post.tags">{{tag.tag_name}}</em>

post.tags是一个数组,因此显示tag_names的最简单方法是使用ng-repeat ......

答案 2 :(得分:3)

这是有效的

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define MAX_ROW_LEN 256
#define MAX_FILENAME_LEN 256

/* Return the start time of the event or -1 if no time. */
static double
get_time(const char *event)
{
  if (!event || event[0] == '%')
    return -1;
  size_t tok = strcspn(event, " ") + 2;
  double ans = strtod(event + tok, NULL);
  if (!ans)
    return -1;
  return ans;
}

/*static inline*/ int
cmp(const void *a, const void *b)
{
  double atime = get_time((char*)a);
  double btime = get_time((char*)b);
  return (atime > btime) - (atime < btime);
}

int
main(int argc, char **argv)
{
  /* process parameters */
  if (argc < 2) {
    fprintf(stderr, "Supply a file to sort.\n");
    exit(EXIT_FAILURE);
  }
  if (strlen(argv[1]) > MAX_FILENAME_LEN) {
    fprintf(stderr, "Filename too long.\n");
    exit(EXIT_FAILURE);
  }
  /* read the file */
  printf("Now processing %s.\n", argv[1]);
  FILE *f = fopen(argv[1], "r");
  if (!f) {
    fprintf(stderr, "Failed to open out. Errno %d.\n", errno);
    exit(EXIT_FAILURE);
  }
  char *trace = malloc(MAX_ROW_LEN);
  char *header = malloc(MAX_ROW_LEN);
  size_t i = 1, j = 1;
  while (fgets(trace + (i-1)*MAX_ROW_LEN, MAX_ROW_LEN, f)) {
    /* (if we can't get the time, it's part of the header) */
    if (get_time(trace + (i-1)*MAX_ROW_LEN) != -1) {
      trace = realloc((void*)trace, (++i)*MAX_ROW_LEN);
    } else {
      strncpy(header + (j-1)*MAX_ROW_LEN, trace + (i-1)*MAX_ROW_LEN,
          MAX_ROW_LEN);
      header = realloc((void*)header, (++j)*MAX_ROW_LEN);
    }
  }
  if (!feof(f)) {
    fprintf(stderr, "Error reading file. Errno %d.\n", ferror(f));
    exit(EXIT_FAILURE);
  }
  printf("Read %zu lines.\n", i);
  fclose(f);
  /* write the header */
  f = fopen("out_fixed", "w");
  if (!f) {
    fprintf(stderr, "Failed to open out_fixed. Errno %d.\n", errno);
    exit(EXIT_FAILURE);
  }
  for (size_t k = 0; k < j-1; ++k) {
    /* (there is '%' in comments, can't print formatted) */
    fputs((void*)(header + k*MAX_ROW_LEN), f);
  }
  /* sort */
  printf("Started sorting.\n");
  time_t start = time(NULL);
  qsort((void*)trace, i-1, MAX_ROW_LEN, cmp);
  printf("Ended sorting, took %fs.\n", difftime(time(NULL), start));
  /* write the sorted trace */
  printf("Started writting to disk.\n");
  start = time(NULL);
  for (size_t k = 0; k < i-1; ++k) {
    fprintf(f, "%s", trace + k*MAX_ROW_LEN);
  }
  printf("Took %fs.\n", difftime(time(NULL), start));
  /* flush */
  printf("Closing file (fflush)\n");
  start = time(NULL);
  if (fclose(f)) {
    fprintf(stderr, "Failed to close out_fixed. Errno %d.\n", errno);
    exit(EXIT_FAILURE);
  }
  printf("Took %fs.\n", difftime(time(NULL), start));
  exit(EXIT_SUCCESS);
}