我試圖在「Controller as」塊中填充一個列表,但我似乎無法得到任何顯示內容。我已經得到了我的想法與$scope
工作小例子:「Controller as」not ng with ng-repeat
JS:
var app = angular.module('testApp', []);
app.controller('listController', ["$scope", ($scope) => {
$scope.listFiles = ["hello", "world"];
}]);
HTML:
<div ng-app="testApp" ng-controller="listController">
<div class="list-group">
<a class="list-group-item list-group-item-action" ng-repeat="filename in listFiles">{{ filename }}</a>
</div>
</div>
但是,當我介紹 「控制器」,該列表不顯示:
JS:
var app = angular.module('testApp', []);
app.controller('listController', ["$scope", ($scope) => {
var $ctrl = this;
$ctrl.listFiles = ["hello", "world"];
}]);
HTML:
<div ng-app="testApp" ng-controller="listController as ctrl">
<div class="list-group">
<a class="list-group-item list-group-item-action" ng-repeat="filename in ctrl.listFiles">{{ filename }}</a>
</div>
</div>
我的理解是$scope
和this
是不同的概念,但我似乎無法環繞在這種情況下,具體我的頭。
這可能是ng-repeat的作用域規則的結果,還是我缺少明顯的東西?
這是否有什麼關係與ES6語法的瀏覽器支持? –
@AlanG根據你的問題,似乎箭頭功能正在工作,否則你會收到一個錯誤'控制器不是功能'。順便說一句,你的問題的主要原因是你正在使用箭頭功能,正如我回答將導致上下文問題。 – Pengyy