And*_*rov 43 angularjs angular-ui angular-ui-bootstrap angularjs-bootstrap
我想创建一个带有pre标签的bootstrap popover,其中包含一个美化的JSON对象.天真的实施,
<span popover='<pre>{[ some_obj | json:" " ]}</pre>'
popover-trigger='mouseenter'>
Run Code Online (Sandbox Code Playgroud)
在将内容插入弹出窗口之前将其转义.使用html内容指定弹出框体的最佳方法是什么?
Mat*_*ian 71
更新:
如可以在被看见这个,你现在应该能够做到这一点没有覆盖缺省模板.
原版的:
至于角度1.2+ ng-bind-html-unsafe
已被删除.你应该使用$ sce服务.参考.
这是一个用于创建可信html的过滤器.
MyApp.filter('unsafe', ['$sce', function ($sce) {
return function (val) {
return $sce.trustAsHtml(val);
};
}]);
Run Code Online (Sandbox Code Playgroud)
这是使用此过滤器覆盖的Angular Bootstrap 0.11.2模板
// update popover template for binding unsafe html
angular.module("template/popover/popover.html", []).run(["$templateCache", function ($templateCache) {
$templateCache.put("template/popover/popover.html",
"<div class=\"popover {{placement}}\" ng-class=\"{ in: isOpen(), fade: animation() }\">\n" +
" <div class=\"arrow\"></div>\n" +
"\n" +
" <div class=\"popover-inner\">\n" +
" <h3 class=\"popover-title\" ng-bind-html=\"title | unsafe\" ng-show=\"title\"></h3>\n" +
" <div class=\"popover-content\"ng-bind-html=\"content | unsafe\"></div>\n" +
" </div>\n" +
"</div>\n" +
"");
}]);
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个Plunker实现.
编辑2:由于这个答案不断得到点击,我会尽可能地保持更新.作为参考这是angular-ui bootstrap repo的模板.如果更改,则覆盖模板将需要匹配更新并添加ng-bind-html=\"title | unsafe\"
和ng-bind-html=\"content | unsafe\"
属性以继续工作.
对于更新的谈话检查这里的问题.
Mic*_*zos 24
如果您使用的角度ui等于或高于0.13.0,则最佳选择是使用该popover-template
指令.以下是如何使用它:
<button popover-template="'popover.html'">My HTML popover</button>
<script type="text/ng-template" id="popover.html">
<div>
Popover content
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
注意:不要忘记模板名称周围的引号popover-template="'popover.html'"
.
作为旁注,可以将popover模板外部化为专用的html文件,而不是<script type="text/ng-template>
像上面那样在元素中声明它.
请参阅第二个演示plunker
我在github项目上发布了一个解决方案:https://github.com/angular-ui/bootstrap/issues/520
我想将此功能添加到您的项目中,这是一个补丁.
添加这些指令:
angular.module("XXX")
.directive("popoverHtmlUnsafePopup", function () {
return {
restrict: "EA",
replace: true,
scope: { title: "@", content: "@", placement: "@", animation: "&", isOpen: "&" },
templateUrl: "template/popover/popover-html-unsafe-popup.html"
};
})
.directive("popoverHtmlUnsafe", [ "$tooltip", function ($tooltip) {
return $tooltip("popoverHtmlUnsafe", "popover", "click");
}]);
Run Code Online (Sandbox Code Playgroud)
并添加模板:
<div class="popover {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
<div class="arrow"></div>
<div class="popover-inner">
<h3 class="popover-title" ng-bind="title" ng-show="title"></h3>
<div class="popover-content" bind-html-unsafe="content"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
用法: <button popover-placement="top" popover-html-unsafe="On the <b>Top!</b>" class="btn btn-default">Top</button>
在plunkr上查看:http ://plnkr.co/edit/VhYAD04ETQsJ2dY3Uum3?p=preview
小智 6
您需要更改默认弹出框模板以指定您要允许Html内容.看看popover-content
div,它现在已经对content
属性进行了绑定,允许不安全的html:
angular.module("template/popover/popover.html", []).run(["$templateCache", function ($templateCache) {
$templateCache.put("template/popover/popover.html",
"<div class='popover {{placement}}' ng-class='{ in: isOpen(), fade: animation() }'>" +
"<div class='arrow'></div><div class='popover-inner'>" +
"<h3 class='popover-title' ng-bind='title' ng-show='title'></h3>" +
"<div class='popover-content' ng-bind-html-unsafe='content'></div>" +
"<button class='btn btn-cancel' ng-click='manualHide()'>Cancel</button>" +
"<button class='btn btn-apply' ng-click='manualHide()'>Apply</button></div></div>");
}]);
Run Code Online (Sandbox Code Playgroud)
对于所有传统的Bootstrap popover
需求,您可以使用以下角度指令.它可以消除HTML模板中的混乱,并且非常易于使用.
您可以配置酥料饼的title
,content
,placement
,淡入/淡出delay
,trigger
事件和内容是否应被视为html
.它还可以防止内容溢出和剪切.
相关的plunker与所有teh代码http://plnkr.co/edit/MOqhJi
撷取画面
用法
<!-- HTML -->
<div ng-model="popup.content" popup="popup.options">Some element</div>
/* JavaScript */
this.popup = {
content: 'Popup content here',
options: {
title: null,
placement: 'right',
delay: { show: 800, hide: 100 }
}
};
Run Code Online (Sandbox Code Playgroud)
JavaScript的
/**
* Popup, a Bootstrap popover wrapper.
*
* Usage:
* <div ng-model="model" popup="options"></div>
*
* Remarks:
* To prevent content overflow and clipping, use CSS
* .popover { word-wrap: break-word; }
* Popup without title and content will not be shown.
*
* @param {String} ngModel popup content
* @param {Object} options popup options
* @param {String} options.title title
* @param {Boolean} options.html content should be treated as html markup
* @param {String} options.placement placement (top, bottom, left or right)
* @param {String} options.trigger trigger event, default is hover
* @param {Object} options.delay milliseconds or { show:<ms>, hide:<ms> }
*/
app.directive('popup', function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {
ngModel: '=',
options: '=popup'
},
link: function(scope, element) {
scope.$watch('ngModel', function(val) {
element.attr('data-content', val);
});
var options = scope.options || {} ;
var title = options.title || null;
var placement = options.placement || 'right';
var html = options.html || false;
var delay = options.delay ? angular.toJson(options.delay) : null;
var trigger = options.trigger || 'hover';
element.attr('title', title);
element.attr('data-placement', placement);
element.attr('data-html', html);
element.attr('data-delay', delay);
element.popover({ trigger: trigger });
}
};
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
83056 次 |
最近记录: |