使用Angularjs更改获取的json数据
change the fetched json data using Angularjs
我已经获取了json数据,看起来像这样:
{
"id": "154",
"user_id": "1445674241",
"title": "Power of one",
"content": "<p><img style='"display: block; margin-left: auto; margin-right: auto;'" src='"/photo.png'" alt='"Power of one'" width='"455'" height='"567'" /></p>'n<p> </p>'n<p>One is really powerful, you are one, God is one, earth is one. Love the one.<img style='"display: block; margin-left: auto; margin-right: auto;'" src='"/photo.png'" alt='"Power of one'" width='"455'" height='"567'" /></p>",
"num_views": "0",
"votes_up": "1",
}
我想使用angular js更改所有img标记的属性(如append-src、change-height&width)。
我还在控制器中使用ng-bind-html呈现html代码。其功能是:
$scope.renderHtml = function (html_code) {
return $sce.trustAsHtml(html_code);
};
帮我更改img标记的属性。
我认为你必须直接在data.content
字符串上工作
类似这样的东西:
var newWidth = 123;
data.content = data.content.replace(/(img.*?width=")(.*?)(")/g, function(c1, c2, c3) { return c1 + newWidth + c3; });
您可以为此创建某种自定义过滤器,例如,如果我从数据输入中获得youtube链接,并且我想在嵌入页面之前对其进行修改-
myApp.filter('trustedURL', ['$sce', function($sce){
return function(input) {
return $sce.trustAsResourceUrl(input);
};
}]);
myApp.filter('youtubeEmb', ['$sce', function($sce){
return function(input) {
var shortYou = input.match(/youtu.be/g);
if(shortYou){
return input.substring(0,input.indexOf('?')).replace(shortYou,"youtube.com/embed");
}else{
return input.substring(0,input.indexOf('&')).replace("watch?v=","embed/");
}
};
}]);
我的HTML-
<iframe src="data.uri | trustedURL | youtubeEmb"></iframe>
这里是url,类似地,您可以为img标记属性创建一些函数。
相关文章:
- 如何将JSON数据导入我的ejs模板
- 如何将json数据显示为html
- Json数据包含日期和时间格式
- 用我的json数据填充JQuery DataTable
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 使用jquery读取Json数据
- 如何将本地json数据加载到Extjs数据模型中
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- React-将jSON数据传递给子组件的子组件
- 尝试从控制器加载带有json数据的Simile时间线
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何在ReactJs中显示Json数据
- 使用javascript进行JSON数据检索
- tu如何将id放在填充了json数据的html表td上
- 将JSON数据从服务器加载到knockout.jsobservable中
- 使用JavaScript处理JSON数据
- 如何将json数据传递到.js文件
- 如何在带有nunjucks和gullow数据的JSON数据中使用带有HTML的内容
- 如何使用var名称调用JSON数据