Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

in jquery code I am trying to add a function in scope and then call that function from a dyncamically created button but ng-click is not being called:

$(document).on("click", ".inlinelbl", function () {
    var $this = $(this);
    var self = this;
    var localScope = angular.element(self).scope();

        if (localScope.InlineSave_Click == undefined) {
            localScope.InlineSave_Click = function (elem, event) {
               //function body
            }
        }

        if (localScope.InlineCancel_Click == undefined) {
            localScope.InlineCancel_Click = function (elem, event) {
                //function body
        }


    var html = "<span class='d'><button class='btn btn-default' ng-click='InlineSave_Click(this, event)' style='display:inline-block;'></button>";
    html += "<button class='btn btn-default' ng-click='InlineCancel_Click()'  style='display:inline-block;'></button></span>";

    var injector = angular.element(document.getElementById('app')).injector();
    var $compile = injector.get('$compile');
    var compiledHtml = $compile(html)(localScope);

    $this.closest("span.s-element").removeClass("hide");
    $this.closest("span.s-element").append($(compiledHtml[0].outerHTML));
}

});

Using chrome's extension for angularjs I have checked that localscope attached with the buttons has both the functions.

What am I missing here?

Thanks in anticipation!

share|improve this question
1  
Try changing the last line to $this.closest("span.s-element").append(compiledHtml);. Getting the HTML back out of the result of the compile sort of defeats the purpose of compiling it. You also might need to call localScope.$apply() afterwards. –  Anthony Chu Jun 26 '14 at 15:59
    
it did the trick. please add it as an answer. –  Haris Jun 27 '14 at 8:54

1 Answer 1

up vote 3 down vote accepted

Instead of taking the HTML from compiled output in this line,

$this.closest("span.s-element").append($(compiledHtml[0].outerHTML));

Append the compiled output as-is...

$this.closest("span.s-element").append(compiledHtml);

You may need to call localScope.$apply() afterwards.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.