gpt4 book ai didi

javascript - 使用 ng-bind-html 的 ng-click 在 Controller 内部不工作

转载 作者:搜寻专家 更新时间:2023-11-01 05:22:05 25 4
gpt4 key购买 nike

在我的代码下方 ng-click 无法正常工作,而我正在检查检查元素 ng-click 未显示,请帮助我如何操作

var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope) {
$scope.firstName = "<b ng-click=test(1)>John</b><br><b ng-click=test1(1)>Testing</b>";


$scope.test=function(val)
{
alert(val)
}

$scope.test1=function(val)
{
alert(val)
}

});
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<span ng-bind-html=firstName><span>
</div>
</body>
</html>

最佳答案

此代码将解决您的问题。在您的 Controller 中添加此指令。

directive('compileTemplate', function($compile, $parse){
return {
link: function(scope, element, attr){
var parsed = $parse(attr.ngBindHtml);
function getStringValue() { return (parsed(scope) || '').toString(); }

//Recompile if the template changes
scope.$watch(getStringValue, function() {
$compile(element, null, -9999)(scope); //The -9999 makes it skip directives so that we do not recompile ourselves
});
}
}
})

您的 HTML 将如下所示:

<div id ="section1" ng-bind-html="divHtmlVariable" compile-template></div>

关于javascript - 使用 ng-bind-html 的 ng-click 在 Controller 内部不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31825405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com