AngularJS- ng-bind-html
richtlijn
Voorbeeld
Bind die innerHTML van die <p>-element aan die veranderlike myText :
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/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>John Doe</h1>";
});
</script>
Definisie en gebruik
Die ng-bind-html
richtlijn is 'n veilige manier om inhoud aan 'n HTML-element te bind.
Wanneer jy AngularJS HTML in jou toepassing laat skryf, moet jy die HTML nagaan vir gevaarlike kode. Deur die "angular-sanitize.js"-module by jou toepassing in te sluit, kan jy dit doen deur die HTML-kode deur die ngSanitize-funksie te laat loop.
Sintaksis
<element ng-bind-html="expression"></element>
Ondersteun deur alle HTML-elemente.
Parameterwaardes
Value | Description |
---|---|
expression | Specifies a variable, or an expression to evaluate. |