【问题标题】:how to create Array of Arrays in angular如何以角度创建数组数组
【发布时间】:2017-04-10 13:38:05
【问题描述】:

我需要一个数组,但我无法得到它,我无法理解我做错了什么..这是我的代码..

c.push({ id: data[0]["id"], title:data[v]["title"], date:data[v]["date"],
         text:data[v]["text"], favorit:"true" });
$scope.favoriti.push({c});
c=[];

我试图用这个来访问数组,但它写的是未定义的

alert( $scope.favoriti[0]["id"] );

我必须写什么,以获得那种我可以通过该警报访问的数组。我真的需要帮助,我尝试了一切。谢谢。

【问题讨论】:

  • 移除$scope.favoriti.push(c)处的大括号;
  • 我需要那种用于 ng-repeat 的数组。删除 {} 无济于事:/
  • 将某些内容推入c 然后立即覆盖整个c 变量是没有意义的。提供minimal reproducible example
  • @charlietfl 为什么?我创建了一个数组(c)我想推入另一个数组?

标签: javascript angularjs arrays


【解决方案1】:

检查此代码。

var app = angular.module('app', []);
app.controller('controller', function($scope) {
  var c = [];
  $scope.favoriti = [];
  var v = 0;
  var data = [{
    id: 1,
    title: 't',
    date: 'd',
    text: 'text'
  }]
  c.push({
    id: data[0]["id"],
    title: data[v]["title"],
    date: data[v]["date"],
    text: data[v]["text"],
    favorit: "true"
  });
  $scope.favoriti.push(c);
  console.log($scope.favoriti);
  console.log("id : " + $scope.favoriti[0][0]["id"]);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
</div>

【讨论】:

    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 2019-04-22
    • 1970-01-01
    • 2018-08-27
    • 2019-04-05
    • 2018-06-28
    相关资源
    最近更新 更多