Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append a html include ng-click in angularjs

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

like image 769
kenza Avatar asked Apr 28 '15 17:04

kenza


2 Answers

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>
like image 101
Mukund Kumar Avatar answered Oct 29 '22 20:10

Mukund Kumar


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));
like image 43
Pankaj Parkar Avatar answered Oct 29 '22 19:10

Pankaj Parkar