Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i have this code in mycontroller i will add this html() to my html, all it right just ng-click dos not work ! have you an ideas why ng-click dos not work

 var html='<div ng-click="selectedValue('+Value+')">Value</div>' ;
     $('#myDiv').html(html);

   $scope.selectedValue = function(value){
     $scope.val =value;

  };

i have to slect the value displayed in the div by using ng-click function selectedValue

share|improve this question
up vote 2 down vote accepted

use this code:

See pluker

Controller:

var html='<div ng-click="selectedValue(value)">Value</div>',
    el = document.getElementById('myID');

$scope.value='mk';

angular.element(el).append( $compile(html)($scope) )

$scope.selectedValue = function (value) {
    $scope.val = value;
    console.log($scope.val)
};

Html:

<body ng-controller="MainCtrl">
    <div id="myID"></div>
</body>
share|improve this answer

You should do DOM manipulation through directive only, Ensure you need to compile element before injecting it.

Code

 var html='<div ng-click="selectedValue('+Value+')">Value</div>' ;
 angular.element(document.getElementByID('myDiv')).append($compile(html)(scope));
share|improve this answer
    
.append works with directive but not .prepend to add any div before matched div element. – Slimshadddyyy 9 hours ago
    
True that. jQuery is already included. Any other alternate in Angular to prepend/inserBefore any element ? Also prepend is part of jqlite - docs.angularjs.org/api/ng/function/angular.element – Slimshadddyyy 9 hours ago
    
@Slimshadddyyy my bad. then prepend should work as well – Pankaj Parkar 9 hours ago
    

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.