ng-click 在控制器内部使用 ng-bind-html 不起作用 [英] ng-click not working inside the controller using of ng-bind-html

查看:29
本文介绍了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)>测试</b>";$scope.test=function(val){警报(val)}$scope.test1=function(val){警报(val)}});

<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><身体><div ng-app="myApp" ng-controller="myCtrl"><span ng-bind-html=firstName><span>

</html>

解决方案

ng-click 不工作的原因是因为,ng-bind-html doens't 编译 div,您应该在那里使用 ng-if OR 编译一个 div 并从指令而不是控制器添加该元素.

标记

<span ng-if=showFirstName><b ng-click=test(1)>John</b><br><b ng-click=test1(1)>测试</b><跨度>

代码

$scope.showFirstName = true;//用于显示div

below my code ng-click not working,while i am checking the inspect element ng-click not showing, help me how to do

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>

解决方案

The reason behind you ng-click not working is because, ng-bind-html doens't compile div, You should use ng-if there OR compile a div and add that element from directive instead of controller.

Markup

<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>

Code

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

这篇关于ng-click 在控制器内部使用 ng-bind-html 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆