1

I am trying to get parameter on click using directive.I want to get child data in the click event for checking has child or not.

.....

html

div ng-app="treeApp">
<ul>
    <treeparent></treeparent>

</ul>

js

(function () {
var treeApp = angular.module('treeApp', []);
treeApp.directive('treeparent', function () {
    return {
        restrict: 'E',
        template: "<button addchild child='m'>ajith</button><div id='new'></div>"
    }
});
treeApp.directive('addchild', function ($compile) {

    return {
        scope: {
            'child':'='
        },
        link: function (scope, element, attrs) {
            debugger;
            element.bind("click", function (scope,attrs) {
                debugger;

//here i want to get hild ie 'm'
 angular.element(document.getElementById('new')).append("<div><button button class='btn btn-default'>new one</button></div>");


                 });
        }
    }

});
})();

plz help me

4
  • 1
    What is the problem? scope.child doesn't work? Commented Jan 5, 2016 at 11:32
  • yes scode.child was undefined Commented Jan 5, 2016 at 11:32
  • It will be scope.child. Also remove this angular.element(document.getElementById('new')).append("<div><button button class='btn btn-default'>new one</button></div>"); - untill you went too far in totally wrong direction. Commented Jan 5, 2016 at 11:34
  • dfsq i want to append that div on click. thats y . can you suggest another way? Commented Jan 5, 2016 at 11:36

2 Answers 2

1

So, i think scope.child is undefined becouse it is overlaps in declaring event.

You can define variable before event binding

    link: function (scope, element, attrs) {
        var child = scope.child;
        element.bind("click", function (scope,attrs) {

           // here you can use child
           console.log('child', child);

        });
    }

or declare different argument names

    link: function ($scope, $element, attrs) {
        element.bind("click", function (scope,attrs) {

            // here you can use $scope.child
            console.log('$scope.child', $scope.child);

        });
    }

Is a callback has scope and attrs argument? May be it has only one $event argument?

    link: function (scope, element, attrs) {

        element.bind("click", function ($event) {

            // here you can use child
            console.log('child', scope.child);

        });
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Anton , sorry its not
callback for bind has only one $event argument
is any way to call directive function from controller
Directive-controller or parent scope controller?
Yes, you can do it i created new answer, becouse comment can be only inline
0

Example for call method from directive in parent scope

parent template

<test-dir data-method="myFunc"></test-dir>
<button ng-click="myFunc('world')">click me</button>

or

<button test-dir data-method="myFunc" ng-click="myFunc('world')">click me</button>

directive

.directive('testDir', function() {
    return {
        scope: {
            method: '=',
        },
        controller : function($scope) {

            $scope.method = function(text) {
                alert('hello '+text);
            };

        },
    };
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.