append a html include ng-click in angularjs

守給你的承諾、 提交于 2019-12-03 02:58:30
Mukund Kumar

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>

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!