如何调用函数以在“ng-repeat”指令中呈现项目
How to call a function to renders items in a `ng-repeat` directive?
我正在编写一个指令来输出项目列表。这些项目旨在从某处的 JSON 读取。
现在,我想根据将传递给指令的方法呈现每个项目。然后,在我的模板中,我调用该方法,将要呈现的项传递给它。
调用方法本身,但传递的项undefined
。
我错在哪里,如何实现这一目标?
您可以在此处查看和使用代码:https://jsfiddle.net/zpntqayr/
这是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.1/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<drop-down-list items="data" display-item="itemToString(item)" />
</div>
<script>
var myApp = angular.module("myApp", []);
myApp.controller("myCtrl", function ($scope, $http) {
$scope.data = [{ "Name": "Value 1", "Value": 1 }, { "Name": "Value 2", "Value": 2 }, { "Name": "Value 3", "Value": 3 }, ] ;
$scope.itemToString = function (item) {
// alert(item);
return item.Name;
};
});
myApp.directive('dropDownList', function () {
return {
restrict: 'E',
replace: true,
scope: {
items: '=',
displayItem: '&'
},
template: '<ul><li ng-repeat="item in items">{{displayItem(item)}}</li></ul>',
};
});
</script>
</body>
</html>
只需替换指令模板中的代码,如下所示:
{{displayItem(item)}}
跟
{{displayItem({item: item})}}
相关文章:
- 在表上使用 ng-repeat 指令
- 带有 ng-repeat 的 Angular JS 指令无法遍历子元素
- ng-repeat图像上的指令仅提取有关第一个图像的信息
- ng-repeat始终呈现为指令模板中的最后一个
- 在angularjs ng-repeat指令中对对象进行排序
- 如何从 Angular ng-repeat 指令中检索数组中的某些索引
- 使用Angular js ng repeat指令过滤json对象
- 如何在angularjs中以正确的方式在指令中使用ng repeat
- 如何让ng repeat在指令模板中工作
- AngularJS ng repeat指令,具有复杂的嵌套数据结构和动态键
- 访问ng repeat指令中的DOM
- ng-repeat=“(key, value)” 不能将 {{key}} 的值用作另一个指令的全局范围的一部分
- 如何更好地利用angular.js应用程序中的ng repeat和指令
- 在指令中使用 ng-model 和 ng-repeat
- 如何调用函数以在“ng-repeat”指令中呈现项目
- 使用 ng-repeat呈现嵌套指令时出错
- 角度指令作用域绑定在 ng-repeat中不起作用
- 如何从 ng-repeat(ng-repeat)中的指令访问控制器范围
- Angular指令ng-repeat input设置焦点为使用add按钮添加的新选项
- AngularJS:指令ng-repeat在link函数中不起作用