我正在使用 AngularJS。我有一个范围变量 $scope.TextToUser
,它以 html 格式打印,在网站上向用户显示。在我的 Controller 中,代码很简单,如下所示;
$scope.TextToUser='UserText'
在相应的html页面上,代码就是{{TextToUser}}
。
我尝试在 Controller 中使用以下代码将文本变为红色;
$scope.TextToUser = '<font size="3" color="red"> 'UserText'</font>'
不幸的是,在 html 页面上,整个字符串都被打印出来,而不是红色的 UserText
。如何将文字变成红色?简单、直接的方法将是首选。
最佳答案
嗯,以下是更合适的:
app = angular.module('<name of your app>', ['ngSanitize']);
app.controller('<controller name>', function($scope, $interpolate){
$scope.Text = 'Test to User In Red';
$scope.TextToUser = $interpolate('<font size="3" color="red">{{Text}}</font>')($scope);
});
现在绑定(bind)html
:
<span ng-bind-html="TextToUser"></span>
您需要包含清理脚本。 https://code.angularjs.org/1.3.2/angular-sanitize.js
注意:- 我尝试使用 $interpolate
但它会在没有 ngSanitize
的情况下给出有关 unsafe
内容的错误。
工作的插头是 here .
关于javascript - 更改 AngularJS 中 $scope 变量中文本的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27030849/