Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Guys I am having problem problem with angular ng-Click.

My Controller:

C#
var ParentController = function ( ) {

    this.MyEvent = function () {
        alert('foo-bar');
    };

    this.AddHTML= function () {
       $("#ButtonSec").html('<input type='button' ng-click='Parent.MyEvent()'                value='inner button'/>');
    };

};

angular.module('myApp', []).controller('ParentController', ParentController);


HTML - 1 (Works as expected button ng-click binds to 'inner button' as expected):

XML
<div ng-app='testComponent'>
     <div id="content" ng-controller="ParentController as Parent">
       <div id="ButtonSec">
            <input type='button' ng-click='Parent.MyEvent()' value='inner button'/>
       </div>
    </div>
</div>



HTML-2 (Not working):When rendering html using Add HTML button. Event not binding to inner button as it should (important thing I can't use $scope as per client requirement)
XML
<div ng-app='testComponent'>
     <div id="content" ng-controller="ParentController as Parent">
       <div id="ButtonSec">

       </div>
    </div>
    <input type='button' ng-click='Parent.AddHTML()' value='Add HTML'/>
</div>



Guys please help .....
Posted
Updated 24-May-15 23:18pm
v2
Comments
Do you see any errors on browser console?
C For Code!!! 25-May-15 5:47am    
no error are there in console.
Set a debugger inside the ParentController and see what is "this" object there.
vinayvraman 26-May-15 6:21am    
I guess the problem is in the way you have constructed the string. It should have been like this

Use double quotes for attribute values instead of single quotes.

$("#ButtonSec").html('<input type="button" ng-click="Parent.MyEvent()" value="inner button"/>');

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900