0

I am trying to append li element using jquery.the "li" element is appended successfully howerver ng-click event is not working. please help.

below is my code.

$(".language-list").append("<li><span ng-click='selectOperator(op)'><label for=checkbox" + $scope.allOperators[op].operator_id +   " class=labeloperator" + ">" + $scope.allOperators[op].operator_name + "</label></div></span></li>");
3
  • where do you run this line? in directive? if yes, $compile it first Commented Feb 6, 2014 at 12:58
  • no not in directive. i am running it on inside controller. Commented Feb 6, 2014 at 13:00
  • I'm sure you know by now that this is not really the right way to write Angular. You should always favour templates over direct DOM manipulation. This looks more like a jQuery plugin. Commented Nov 27, 2015 at 13:06

2 Answers 2

5

On append you need compile the element.

It not good practice to do that in controller. Use directive for any DOM selection/manipulation.

Anyways here is example:

var elmnt = angular.element(/* ... */);


elmnt.append(
           $compile(
           "<li><span ng-click='selectOperator(op)'><label for=checkbox" + $scope.allOperators[op].operator_id +   " class=labeloperator" + ">" + $scope.allOperators[op].operator_name + "</label></div></span></li>"
               )($scope));
Sign up to request clarification or add additional context in comments.

Comments

1

You must compile the dynamic HTML to do the work.

$(".language-list")
  .append(
        $compile(
           "<li><span ng-click='selectOperator(op)'><label for=checkbox" + 
            $scope.allOperators[op].operator_id +   " class=labeloperator" + ">" +
            $scope.allOperators[op].operator_name + "</label></div></span></li>")
 )($scope);

For more Information about $compile service

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.