使用简单数组进行基本ng-repeat

时间:2015-09-02 20:08:43

标签: javascript arrays angularjs angularjs-ng-repeat

对于我的生活,我无法弄清楚为什么我的简单一维数组不会使用ng-repeat打印出来。我已经尝试了十几种不同的组合,以至于我失去了this关键字的含义。

http://jsfiddle.net/ts8yeynk/1/

<body ng-app="test">
    <p>{{ "Hello" + ", world!" }}</p>
    <p>{{ poop }}</p>
    <p>6 + 4 = {{ 6 + 4 }}</p>
    <div ng-controller="MainCtrl as m">
        <div ng-repeat="x in m.linkList">x is: {{x}}</div>
        <div ng-repeat="x in m.list1">x is: {{x}}</div>
        <div ng-repeat="x in m.clown">x is: {{x}}</div>
        <div ng-repeat="x in m.sinkList">x is {{x}}</div>
        <div ng-repeat="x in m.fart">x is {{x}}</div>
        <div ng-repeat="x in m.boom">x is {{x}}</div>
    </div>
</body>

app.js

var app = angular.module('test', []);
var poop = "Hello, World!";
app.controller('MainCtrl', function($scope) {
    var aList = this.list2;
    var list1 = [1, 2, 3, 4];

    $scope.boom = [1,2,3,4];


    this.clown = list2;

    fart = [1,2,3,4];

    $scope.linkList = list2;
    $scope.sinkList = [1, 2, 3,4];
});


var list1 = [1, 2, 3, 4];
var list2 = [that, there, and, overe, here];

老实说,我只是在尝试一切。我很难调试角度。

在绿色的地球上我做错了什么????

2 个答案:

答案 0 :(得分:1)

使用controller as语法 - 您在控制器中引用this,而不是$scope

//In the controller
this.linkList = [1,2,3,4];

查看:

<p ng-repeat="x in linkList">{{x}}</p>

演示:http://jsfiddle.net/ts8yeynk/2/

如果您检查控制台,则可以看到出现的错误(that未定义等等)

答案 1 :(得分:0)

如果不使用controller as语法,我会将$scope引用为:

<强> app.js

var app = angular.module('test', []);
app.controller('MainCtrl', function($scope) {
    $scope.theList = list1;
});

var list1 = [1,2,3,4];

查看

<body ng-app="test">
    <div ng-controller="MainCtrl">
        <div ng-repeat="j in theList">{{ j }}</div>
    </div>
</body>