ng-repeat表示由变量表示的数组

时间:2016-05-20 20:11:38

标签: javascript angularjs ng-repeat

我正在尝试使用ng-repeat来显示数组。根据变量的值,数组ng-repeat循环应该改变。

这是我到目前为止所做的事情(不起作用)

HTML

<body ng-app="myApp" ng-controller="myCtrl">

<h1 ng-repeat="x in selection">{{x}}</h1>

JS

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records1 = [
    "Apple",
    "Orange",
    "Banana",
    "Mango",
  ];
  $scope.records2 = [
     "Lion",
     "Tiger",
     "Crocodile",
     "Olephant"
  ];
  $scope.selection = records1;

});

我在这里做错了什么,以及实现这个目标的正确方法是什么?

1 个答案:

答案 0 :(得分:4)

我认为你应该这样做:

<强> JS

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records1 = [
    "Apple",
    "Orange",
    "Banana",
    "Mango",
  ];
  $scope.records2 = [
     "Lion",
     "Tiger",
     "Crocodile",
     "Olephant"
  ];
  $scope.selection = $scope.records1;

});