编程语言
首页 > 编程语言> > javascript – ng-click使用ng-bind-html在控制器内部无法正常工作

javascript – ng-click使用ng-bind-html在控制器内部无法正常工作

作者:互联网

在我的代码下面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>

解决方法:

你点击不工作的原因是因为,ng-bind-html不能编译div,你应该使用ng-if OR编译div并从指令而不是控制器添加该元素.

标记

<div ng-app="myApp" ng-controller="myCtrl">
  <span ng-if=showFirstName>
    <b ng-click=test(1)>John</b><br><b ng-click=test1(1)>Testing</b>
  <span>
</div>

$scope.showFirstName = true;//for showing div

标签:javascript,angularjs,html5,angularjs-controller,ng-bind-html
来源: https://codeday.me/bug/20190611/1221990.html