角度JS -如何在控制器中清除HTML ?

时间:2022-05-30 20:11:12

I'm trying to sanitize HTML in the controller as I'm trying to update the document.title dynamically with the title of the post. (I know that for SEO purposes this isn't recommended but I need to use it here)

当我试图更新文档时,我试图在控制器中清除HTML。动态标题与文章标题。(我知道出于搜索引擎优化的目的,不推荐使用它,但我需要在这里使用它)

$scope.prevTitle = "dynamic title gets pulled in here &"
document.title = $scope.prevTitle 

For this example, I've just used a random HTML entity. I've tried the parseAsHtml method from the official documentation but I'm having no luck. I tried the following:

在本例中,我使用了一个随机的HTML实体。我已经尝试了官方文档中的parseAsHtml方法,但是我没有运气。我试着以下:

document.title = $sce.parseAsHtml($scope.prevTitle)

But no luck. The documentation suggests it needs to be used within a function. Any suggestions on how I would acheive this?

但没有运气。文档表明它需要在函数中使用。有什么建议吗?

A console log of the above ( console.log($sce.parseAsHtml($scope.prevTitle)) ) would return:

上面的控制台日志(console.log($sc . parseashtml ($scope.prevTitle)))将返回:

function (b,c){return e.getTrusted(a,d(b,c))} 

3 个解决方案

#1


2  

$sanitize can be used as @acg pointed out. Alternatively, you can use it directly with the ng-bind-html directive where it automatically sanitizes the output variable before rendering the output.

$sanitize可以使用@acg指出。或者,您可以直接使用它与ng-bind-html指令一起使用,在该指令中,在呈现输出之前,它会自动地清理输出变量。

The above point is not quite clear in the documentation, but there is a fairly extensive example in it with which you can play in pluncker.

上面的这一点在文档中不是很清楚,但是在其中有一个相当广泛的示例,您可以使用它在pluncker身上玩。

Please also bear in mind that ngSanitize is an external module and you need to explicitly load angular-sanitize.js or include it in your js minification.

请记住,ngSanitize是一个外部模块,您需要显式地加载angular-sanitize。或者将它包含在你的js压缩中。

#2


1  

Use $sanitise and trustAsHtml instead

使用$sanitise和trustAsHtml代替

First of all inject 'ngSanitize' in your module

首先,在模块中注入“ngSanitize”

Now in your controller, just add

现在在控制器中,添加

$scope.prevTitle = "dynamic title gets pulled in here &"
document.title = $sce.trustAsHtml($scope.prevTitle)

#3


0  

If you want to sanitize the html returned, I would think it would be as simple as using the $sanitize service:

如果您想要对返回的html进行消毒,我认为它会像使用$sanitize服务一样简单:

document.title = $sanitize($sce.parseAsHtml($scope.prevTitle))

#1


2  

$sanitize can be used as @acg pointed out. Alternatively, you can use it directly with the ng-bind-html directive where it automatically sanitizes the output variable before rendering the output.

$sanitize可以使用@acg指出。或者,您可以直接使用它与ng-bind-html指令一起使用,在该指令中,在呈现输出之前,它会自动地清理输出变量。

The above point is not quite clear in the documentation, but there is a fairly extensive example in it with which you can play in pluncker.

上面的这一点在文档中不是很清楚,但是在其中有一个相当广泛的示例,您可以使用它在pluncker身上玩。

Please also bear in mind that ngSanitize is an external module and you need to explicitly load angular-sanitize.js or include it in your js minification.

请记住,ngSanitize是一个外部模块,您需要显式地加载angular-sanitize。或者将它包含在你的js压缩中。

#2


1  

Use $sanitise and trustAsHtml instead

使用$sanitise和trustAsHtml代替

First of all inject 'ngSanitize' in your module

首先,在模块中注入“ngSanitize”

Now in your controller, just add

现在在控制器中,添加

$scope.prevTitle = "dynamic title gets pulled in here &"
document.title = $sce.trustAsHtml($scope.prevTitle)

#3


0  

If you want to sanitize the html returned, I would think it would be as simple as using the $sanitize service:

如果您想要对返回的html进行消毒,我认为它会像使用$sanitize服务一样简单:

document.title = $sanitize($sce.parseAsHtml($scope.prevTitle))