所以我有一个有趣的问题,但我还没有找到答案。
假设我从 JSON 文件中获得了一堆数据,但不知何故,其中一个主要字段如下所示
description : '<img src="http://o.aolcdn.com/hss/storage/midas/37c38989a5f26031be3e0ec5ffc5cd2/200012386/got-hbo-go-crash-2014-04-07-01_thumbnail.jpg"/> Viewing of the Game of Thrones season debut came to a crashing halt yesterday thanks to server problems with HBO Go. The cable outfit first reported the problem late yesterday via Twitter, and finally restored full service early this morning. That...'
当使用 ng-bind-html 将 json 数据绑定到我的页面上的 innerHTML 时,我如何能够从该 IMG 获取 src。 (在 img 本身上使用 display:none; 这样就不会显示)
我尝试过 getElementsByTags 等,但没有返回有效值。
是否有某种“有角度”的方式来做到这一点或者......?
BR's
根据存储在对象中的 html,您可以执行以下操作:
var json = {description : '<img src="http://o.aolcdn.com/hss/storage/midas/37c38989a5f26031be3e0ec5ffc5cd2/200012386/got-hbo-go-crash-2014-04-07-01_thumbnail.jpg"/> Viewing of the Game of Thrones season debut came to a crashing halt yesterday thanks to server problems with HBO Go. The cable outfit first reported the problem late yesterday via Twitter, and finally restored full service early this morning. That...'};
$scope.imgPath = '';
$scope.getImage = function() {
el = angular.element(angular.element('<div>' + json.description + '</div>').find('img')[0]);
$scope.imgPath = el.attr('src');
}
这是一个演示:http://plnkr.co/edit/JXy97lrN5FyW1MK33qO1?p=preview http://plnkr.co/edit/JXy97lrN5FyW1MK33qO1?p=preview
此解决方案假设 jQuery 不包含在项目中。
这也有效:
$scope.imgPath = angular.element(json.description).attr('src');
Demo: http://plnkr.co/edit/zVllp9bmsU2DoZwDZnCY?p=info http://plnkr.co/edit/zVllp9bmsU2DoZwDZnCY?p=info
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)