0
Below is my code

I created a simple page and include a directive in it. And on ng-click in directive i want to call parent scope's method.

<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>

So my console log print as "success : undefined" So content object not passing from directive scope to parentscope.

Please help me.

Sunny
  • 308
  • 2
  • 14

1 Answers1

1

I believe your call inside template should either be:

<a href="#" ng-click="onItemClick({'content':content})">

or

<a href="#" ng-click="onItemClick({'obj':content})">

Can you try. For more details see this SO post Can an angular directive pass arguments to functions in expressions specified in the directive's attributes?

Community
  • 1
  • 1
Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • Hello Chandermani. I have another question as i want to pass second argument in above function with obj. So help me what is the syntax? – Sunny Oct 21 '14 at 08:06
  • Data has to be passed in the object format `{}` when calling the linked function from directive code. – Chandermani Oct 21 '14 at 09:00