如何在字符串中使用 br 以使用 html 标签正确显示我的字符串?
how can i use br in a string to show my string correctly with the html tag?
我的字符串是:
$scope.lyrics = 'blablablalba\nblablabla\nblablabla';
然后我将 '\n'
替换为 <br>
标签
现在我的字符串是:
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
当我的字符串在 html:
<p> {{ lyrics }} </p>
我的结果是
blablablalba <br> blablabla <br> blablabla
我能为我的结果做些什么:
lablablalba
blablabla
blablabla
您必须清理 HTML 标签。
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
<p ng-bind-html-unsafe="lyrics">
输出将是:
blablablalba
blablabla
blablabla
Angular 将字符串作为文本插入以确保安全,您需要指定以安全地允许绕过无效内容。
ngBindHtml
- directive in module ng
Evaluates the expression and inserts the resulting HTML into the
element in a secure way. By default, the resulting HTML content will
be sanitized using the $sanitize service. To utilize this
functionality, ensure that $sanitize is available,
工作示例:
var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope) {
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="https://rawgit.com/angular/bower-angular-sanitize/master/angular-sanitize.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<p ng-bind-html="lyrics"></p>
</body>
</html>
我的字符串是:
$scope.lyrics = 'blablablalba\nblablabla\nblablabla';
然后我将 '\n'
替换为 <br>
标签
现在我的字符串是:
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
当我的字符串在 html:
<p> {{ lyrics }} </p>
我的结果是
blablablalba <br> blablabla <br> blablabla
我能为我的结果做些什么:
lablablalba
blablabla
blablabla
您必须清理 HTML 标签。
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
<p ng-bind-html-unsafe="lyrics">
输出将是:
blablablalba
blablabla
blablabla
Angular 将字符串作为文本插入以确保安全,您需要指定以安全地允许绕过无效内容。
ngBindHtml - directive in module ng
Evaluates the expression and inserts the resulting HTML into the element in a secure way. By default, the resulting HTML content will be sanitized using the $sanitize service. To utilize this functionality, ensure that $sanitize is available,
工作示例:
var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope) {
$scope.lyrics = 'blablablalba<br>blablabla<br>blablabla';
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="https://rawgit.com/angular/bower-angular-sanitize/master/angular-sanitize.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<p ng-bind-html="lyrics"></p>
</body>
</html>