【发布时间】:2015-02-04 05:38:03
【问题描述】:
使用 ng-repeat 将作用域变量传递给指令模板的正确方法是什么?我正在尝试遍历数据数组以呈现一些包含 templateUrls 的“app”元素。如果我没有在指令中设置 scope : true ,则模板变量为空。将 ng-repeat 中定义的变量传递给模板而不污染范围的正确方法是什么?
// controller
userApp.controller('mainController', ['$scope', 'mainFactory', 'credsFactory',
function($scope, mainFactory, credsFactory) {
var findAppsData = function() {
mainFactory.findAppsData()
.success(function (data) {
$scope.appsData = data;
}).
error(function(error) {
$scope.status = 'Unable to get data: ' + error.message;
});
};
findAppsData();
}]);
// directive
userApp.directive('app', function(){
return {
restrict : "E",
scope : {}, // vars referenced in template are empty.
scope : true // vars are inherited in template.
templateUrl : 'templates/app-detail.html'
}
})
// index.html
<app ng-repeat="app in appsData"></app>
// app-detail.html
<span class="rtl">{{ app.appDisplayName }}</span>
<span class="rtl"> | {{ app.categoryId }}</span>
<br />
<span class="rtl">{{ app.description }}</span>
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat