AngulaJs ng单击表达式html [英] AngulaJs ng-click in expression html
问题描述
我正在AngularJS表达式中显示HTML.
I am displaying HTML in a AngularJS expression.
在我的HTML"中
<div ng-bind-html="myText"></div>
在我的控制器中
$scope.myText = $sce.trustAsHtml("<div class='my-style' ng-click='onClick(10)'>click me</div>");
$scope.onClick = function(value)
{
console.log("onClick: value:"+value);
}
但是我无法获得点击事件
But I am not able to get the click event
更新
在一个问题之一中,我看到它已经描述了如何在文本中添加HTML标签或如何编译HTML.但是我的问题与如何从编译的HTML中获取ng-click事件有关.我看不到提供此解决方案的任何答案.
In one of the question I see it has be described about how to add HTML tags in the text or how to compile HTML. But my question is related to how to get ng-click event from the complied HTML. I don't see any of the answers providing this solution.
推荐答案
很抱歉再次编写代码:首先给您的div指定一个唯一的ID:
Sorry for again writing the code : First give a unique id to your div :
<div id="test"></div>
然后将该代码更改为以下代码:
Then Change that code to below one:
var myText = "<div class='my-style' ng-click='onClick(10)'>click me</div>";
var element = angular.element(document.querySelector('#test'));
var generated = element.html(myText);
$compile(generated.contents())($scope);
这将管理您的代码正常工作.欢呼!
This will manage your code to work. cheers!
这篇关于AngulaJs ng单击表达式html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!