AngularJS ng-bind-html-instructie

Definitie en gebruik

ng-bind-html Instructies zijn een veilige manier om inhoud te koppelen aan HTML-elementen.

Wanneer je AngularJS gebruikt om HTML in je applicatie te schrijven, moet je controleren of er gevaarlijke code in de HTML zit. Door de module "angular-sanitize.js" in je applicatie op te nemen, kun je HTML-code uitvoeren met de ngSanitize-functie om te controleren.

Voorbeeld

Bind de innerHTML van het <p>-element aan de variabele myText:

<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-sanitize.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
    <p ng-bind-html="myText"></p>
</div>
<script>
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
    $scope.myText = "My name is: <h1>Bill Gates</h1>";
});
</script>

Try it yourself

Syntax

<element ng-bind-html="expression</element>

All HTML elements are supported.

Parameters

Parameters Description
expression Specify the variable or expression to be calculated.