AngularJS:ng-bind-html不适用于按钮标记 [英] AngularJS: ng-bind-html doesn't work with button tag

查看:83
本文介绍了AngularJS:ng-bind-html不适用于按钮标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在divng-bind-html中动态打印输入类型按钮时遇到问题。

I am having problem to print out an input type button dynamically in a div "ng-bind-html".

HTML模板:

<input type="button" value="Add" ng-click="add()">
<div ng-bind-html="snippet"></div>

控制器:

$scope.add = function() {
   $scope.snippet = "<input type='button' value='Test' ng-click='myFunc()'><b>Test 2</b>";
}

删除标签输入,然后我只看到粗体文字测试2。

The tag input is removed and then I see just the "bold" text Test 2.

谢谢

推荐答案

出于某种原因你的html标签是用angular js标记为 unsafe 。如果您确定您的代码段文本是安全的,则可以在将 $ scope.nippet

For some reason your html tag is mark as unsafe by angular js. If you sure that your snippet text is safe, you can $sce.trustAsHtml it before adding it to the $scope.snippet.

app.controller('yourCtrl', ['$scope', '$sce', function($scope, $sce){
    $scope.add = function(){
        var text = "<input type='button' value='Test'><b>Test 2</b>";

        // mark it as clean
        text = $sce.trustAsHtml(text);

        $scope.snippet = text;
    };
}]);

这篇关于AngularJS:ng-bind-html不适用于按钮标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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