Below is my code
我创建了一个简单的页面并在其中包含一个指令。在ng-click in指令中,我想调用父作用域的方法。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Directive Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"> </script>
</head>
<body>
<div ng-app="myapp" ng-controller="myController">
<ul-dir on-item-click="itemClick(obj)"></ul-dir>
</div>
</body>
</html>
<script>
var myapp = angular.module("myapp", []);
myapp.directive('ulDir', function() {
return {
restrict: 'E',
replace: 'true',
template: '<div id="container"><ul><li ng-repeat="content in contents"><a href="#" ng-click="onItemClick(content)">{{content.name}}</a></li></ul></div>',
controller: function ($scope) {
$scope.contents = [{'name':'Nishu', 'age':'20'},{'name':'Nidhi', 'age':'21'},{'name':'Kirti', 'age':'24'}];
},
scope: {
onItemClick: '&' // Pass a reference to the method
}
}
});
myapp.controller('myController', function($scope) {
$scope.itemClick = function(content){
console.log("Success : "+content);
};
});
</script>
所以我的控制台日志打印为“success:undefined” 所以内容对象没有从指令范围传递给parentcope。
请帮帮我。
答案 0 :(得分:1)
我相信你在模板中的调用应该是:
<a href="#" ng-click="onItemClick({'content':content})">
或
<a href="#" ng-click="onItemClick({'obj':content})">