【问题标题】:Access variable from object in array in the angularJS code从angularJS代码中的数组中的对象访问变量
【发布时间】:2018-01-02 18:40:22
【问题描述】:

我是 typescript 和 angularJS 的新手,我创建了一个类似的类

[*.ts]
export class Test{
      test: string;
      constructor(foo: string){
        this.test = foo;
      }
}

我现在想创建几个测试对象并将它们推送到一个数组中

[*.ts]
arr = [];
arr.push(new Test("dummy"));
arr.push(new Test("dummy2"));

在 html 部分,我现在想遍历测试数组并访问“测试”变量:

  [*.html]
  <item  *ngFor="let i of arr">
        {{i.test}}
  </item>

但这似乎不起作用。对于熟悉打字稿的人来说,这应该是显而易见的,但对我来说却不是。你得到我想做的事了吗?你有什么建议吗?

非常感谢!

【问题讨论】:

  • 我认为您在这里混为一谈... HTML 中没有“循环”,这是框架为您提供的。 *ngFor 是一个 Angular 的东西。你在使用 Angular 和 TypeScript,是吗?如果是这样,您应该将其标记为 Angular。
  • 其实你是对的。我使用角度和打字稿。所有这些不同的框架和语言有点令人困惑......对不起!

标签: javascript html arrays angular typescript


【解决方案1】:

您将对象推送到错误的变量,应该是,

arr = [];
arr.push(new Test("dummy"));
arr.push(new Test("dummy2"));

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多