javascript 如何获取用 $sce 包装的对象的未包装值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24310607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:33:49  来源:igfitidea点击:

How to get unwrapped value of object that wrapped with $sce?

javascriptangularjs

提问by ali

In my web application I given an object that is wrapped with angular $sce.In this object each property has a special property $$unwrappedTrusted but it doesn't really give me the object value.How I can get trusted value of object property?

在我的 web 应用程序中,我给出了一个用 angular $sce 包裹的对象。在这个对象中,每个属性都有一个特殊的属性 $$unwrappedTrusted 但它并没有真正给我对象值。我如何获得对象属性的可信值?

回答by Ninja

As @rrhrg said properties start with $$ considered as private and not safe to use. Better use valueOf method of $sce service.

正如@rrhrg 所说,以 $$ 开头的属性被认为是私有的,使用起来不安全。更好地使用 $sce 服务的 valueOf 方法。

var trustedResource = $sce.trustAsResourceUrl("www.abcd.com/folder/image.png");
$sce.valueOf(trustedResource); // "www.abcd.com/folder/image.png"

http://jsbin.com/morixekuxi/edit?html,js,console

http://jsbin.com/morixekuxi/edit?html,js,console

回答by James

You can use the getTrustedHtml() function. This gets the html string value from $$unwrapTrustedValue.

您可以使用 getTrustedHtml() 函数。这从 $$unwrapTrustedValue 获取 html 字符串值。

vm.user.bio = $sce.getTrustedHtml(vm.user.bio);

You may also need to include ngSanitize.

您可能还需要包含ngSanitize

There are more answers on this topic in this post: Unit testing the output of $sce.trustAsHtml in Angular

在这篇文章中有关于这个主题的更多答案: 在 Angular 中单元测试 $sce.trustAsHtml 的输出

回答by rrhrg

If I understood your question correctly, you would like to unwrap (or use?) a value wrapped by angular's service $sec.

如果我正确理解了您的问题,您想解开(或使用?)由 angular 的 service 包装的值$sec

To use such a value in your html document, you can use the directive ng-bind-html.

要在您的 html 文档中使用这样的值,您可以使用指令ng-bind-html

<div ng-bind-html="myWrappedValue"></div>

To unwrap such a value, you may use the function $$unwrapTrustedValue().

要解开这样的值,您可以使用函数$$unwrapTrustedValue()

$sce.trustAsHtml('&copy;').$$unwrapTrustedValue() === '&copy;'

Please notethat properties which start with two dollar signs ($$) are considered as private and should NOTbe accessed!

请注意,这两个美元符号($$)被认为是私人的开端,应该是性质被访问!

See also http://jsfiddle.net/rrhrg/9vzckoj8/

另见http://jsfiddle.net/rrhrg/9vzckoj8/

回答by ali

I get value of each property by calling toString() function but I did not found any method that help me to find object itself.

我通过调用 toString() 函数来获取每个属性的值,但是我没有找到任何可以帮助我找到对象本身的方法。