0
.directive('mydirective', [function($scope, $document,windowService) {
return{
    link : function(scope,element,attars){
        --- Some more code —--

        var containers = $('.container’);
        containers.bind('click', function(event) {
            var elem = event.currentTarget;
            elem.append('<div>test</div>’); //Appending is failing
        });
    }
}]);

TypeError: 'undefined' is not a function (evaluating 'elem.append('<div>test</div>')')    

I am just starting off with AngularJS and stuck with the above issue, I am trying to append a div to the container.

2 Answers 2

3

Try with this

containers.bind('click', function (event) {
        var elem = event.currentTarget;
        $(elem).append('<div>test</div>’); //Appending should work
}

as elem can be the HTML input object you have to convert it into jQuery object to use .append() method of jQuery! so wrap your elem variable arround $(). It should work

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

Comments

0

Better to use this. Removes dependency to JQuery as Angular core only use JQLite. It is basically the same thing that happens. https://docs.angularjs.org/api/ng/function/angular.element

containers.bind('click', function (event) {
        var elem = event.currentTarget;
        angular.element(elem).append('<div>test</div>’); //Appending should work
}

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.