gpt4 book ai didi

javascript - 允许 Angular Controller 或 View 中使用 html 标签

转载 作者:行者123 更新时间:2023-12-02 16:19:48 25 4
gpt4 key购买 nike

我的 Controller :

LandingApp.controller('LandingCtrl2', function($scope){
$scope.articles = [
{
'imageSrc' : IMG_DIR + 'spec9.jpg',
'title' : 'Stencils',
'description': 'Plastic or thin metal stencils.<br/>100% Custom made'
}
];
});

当我尝试在My ng中重复(...文章中的文章...){{article.description}}我得到了... stencils.<br/>100% .... ,但我需要断线或其他一些 html 标签。我该如何解决这个问题?

最佳答案

您需要创建一个方法,它可以告诉 AngularJS 将该字符串呈现为 HTML

示例:

$scope.renderHtml = function (htmlCode) {
return $sce.trustAsHtml(htmlCode);
}

在您的 View 中:

<div ng-bind-html="renderHtml(article.description)"></div>

不要忘记在 Controller 中包含 $sce

LandingApp.controller('LandingCtrl2', function($scope,$sce){ ...

关于javascript - 允许 Angular Controller 或 View 中使用 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29231979/

25 4 0