对于我一生,我无法弄清楚为什么我的简单一维数组无法使用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];
老实说,我正在尝试一切。我很难调试角度。
我在神绿色地球上做错了什么????
最佳答案
使用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
等)