简体   繁体   English

AngularJS:ng-bind-html不适用于按钮标记

[英]AngularJS: ng-bind-html doesn't work with button tag

I am having problem to print out an input type button dynamically in a div "ng-bind-html". 我有问题在div“ng-bind-html”中动态打印输入类型按钮。

HTML template: HTML模板:

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

Controller: 控制器:

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

The tag input is removed and then I see just the "bold" text Test 2. 标签输入被删除,然后我只看到“粗体”文本测试2。

Thanks 谢谢

For some reason your html tag is mark as unsafe by angular js. 由于某种原因,您的html标记被角度js标记为unsafe If you sure that your snippet text is safe, you can $sce.trustAsHtml it before adding it to the $scope.snippet . 如果您确定您的代码段文本是安全的,则可以在将其添加到$scope.snippet之前将$sce.trustAsHtml添加到其中。

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;
    };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM