【发布时间】:2015-07-10 20:15:48
【问题描述】:
jsFiddle 解释了一切
我的代码分隔了主题标签,例如 #riots = <span class="hashtags">#riots</span>,但它被打印为纯文本而不是 html。我做错了什么?
function formCtrl($scope){
$scope.$watch(function() {
$scope.description = "Wow, it's crazy over here! #baltimore #riots";
$scope.vidTags = $scope.description.match(/(^|\s)(#[a-z\d-]+)/ig);
$scope.desc = $scope.description.replace(/(^|\s)(#[a-z\d-]+)/ig, "$1<span class='hashtag'>$2</span>");
})
}
#description {
width: 300px;
height: 3em;
}
.hashtag {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="formCtrl">
<textarea ng-model="description" id="description"></textarea>
<br />
vidTags: {{vidTags}}
<br />
desc: {{desc}}
<br />
</div>
【问题讨论】:
-
读取 $sce 和 $santize docs.angularjs.org/api/ng/service/$sce
-
感谢您的评论@RenaissanceProgrammer。答案没有帮助,但它下面的第一条评论有帮助!现在,我只想让它工作。稍后,我会尝试让 $sce 或 $sanatize 工作
-
更正:'ng-bind-html-unsafe' 仅适用于 Angularjs 1.1 或更早版本。
标签: javascript regex angularjs hashtag