如何在角用户界面引导警报中包括链接?
企图:
<div ng-controller="AlertDemoCtrl">
<alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">{{alert.msg}}</alert>
<button class='btn' ng-click="addAlert()">Add Alert</button>
</div>
脚本
function AlertDemoCtrl($scope) {
$scope.alerts = [
{ type: 'error', msg: 'Oh snap! Change a few things up and try submitting again.' },
{ type: 'success', msg: '<a href="">Well done!</a> You successfully read this important alert message.' }
];
$scope.addAlert = function() {
$scope.alerts.push({msg: "Another alert!"});
};
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
}
发布于 2013-07-04 12:03:34
在AngularJS表达式中嵌入HTML通常不是最好的方法,因为这样就无法计算AngularJS指令。
不管怎么说,回到你的问题-有很多方法来解决你的问题。如果您在显示链接之后,最简单的方法是使用ng-bind-html
指令(http://docs.angularjs.org/api/ngSanitize.directive:ngBindHtml):
<alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
<span ng-bind-html="alert.msg"></span>
</alert>
发布于 2014-11-20 11:04:46
以上所需的答案需要更多的东西才能为我工作。
我的警报是这样的:
$scope.alerter.add({
type: "danger",
msg: "<strong>ERROR:</strong> Unable to load credit care plan. Please contact your administrator."
});
完成上述操作后,我开始得到以下错误:
Error: [$sce:unsafe]
所以我继续,给我做了一个过滤器来绕过$sce安全。我把过滤器命名为不安全的:
.filter('unsafe', function($sce) {
return function(value) {
if (!value) { return ''; }
return $sce.trustAsHtml(value);
};
})
然后像这样使用过滤器:
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">
<span ng-bind-html="alert.msg | unsafe"></span>
</alert>
我的closeAlert函数如下所示:
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
https://stackoverflow.com/questions/17475728
复制相似问题