最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: ng-bind-html doesn't work with button tag - Stack Overflow

programmeradmin3浏览0评论

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

HTML template:

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

Thanks

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

HTML template:

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

Thanks

Share Improve this question edited Mar 30, 2014 at 13:51 Danilo Paone asked Mar 30, 2014 at 13:10 Danilo PaoneDanilo Paone 1481 gold badge2 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

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;
    };
}]);
发布评论

评论列表(0)

  1. 暂无评论