1

I have an array of

invitationList = ["A","B","C","D"]

from this array, I need to form a list having a name of A/B/C/D with button of accept. While clicking this accept button, accept function should be invoked.

I tried,

 var list_Invitation='';
 list_Invitation="<ul class='offline-invitation'>";
 var i = 1;
    for(var index in invitationList)
        {
        i++;
        list_Invitation=list_Invitation+'<li class="class_InvitationList"><h5>'+invitationList[index]+'</h5><input type="button" class="accept-btn" value="Accept" ng-click="acceptInvitation(\''+invitationList[index]+'\', \'accept-btn'+i+'\');"/></li>';
        }
            list_Invitation=list_Invitation+"</ul>";


$scope.acceptInvitation=function(name,id)
  {
      console.log('Invoked');
  }

List will be shown, but the function cannot be invoked while clicking the button.

Can anyone help me to resolve this?

7
  • do you get any errors in the console? Commented Mar 4, 2014 at 4:52
  • There is no error in console. Commented Mar 4, 2014 at 4:53
  • ok, what does final html look like? Commented Mar 4, 2014 at 4:54
  • Final Html Commented Mar 4, 2014 at 4:59
  • why are you using dom manipulation when you should be using ng-repeat ... if you create your own dom then you will have to compile the structure Commented Mar 4, 2014 at 4:59

1 Answer 1

2

The angular way is

<ul class='offline-invitation'>
    <li class="class_InvitationList" ng-repeat="item in invitationList">
         <h5>{{item}}</h5>
        <input type="button" class="accept-btn" value="Accept" ng-click="acceptInvitation(item, $index)" />
    </li>
</ul>

then in your controller

$scope.invitationList = ["A", "B", "C", "D"];

$scope.acceptInvitation = function (name, id) {
    console.log('Invoked', name, id);
}

Demo: Fiddle

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

9 Comments

+1, It's really good, But the OP asked about dynamic function
@RameshRajendran Yes that is the caption... but from the question accept function should be invoked, I think OP just created the dom structure and appended it to the dom without compiling it... so the ng-click is not getting invoked
Okay, I will test this locally and will give solution to OP, cool
@RameshRajendran you can always use the attached fiddle to test the cases
@Vinod jsfiddle.net/arunpjohny/d8CD9/10 - as the original problem is solved it will be better to ask a new question - it will increase the possibility of an answer because only I will be seeing this question
|

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.