簡體   English   中英

在AngularJS的$ scope變量中更改文本的字體顏色

[英]Change font color of text in $scope variable in AngularJS

我正在使用AngularJS。 我有一個范圍變量$scope.TextToUser ,該變量以html打印,可在網站上顯示給用戶。 在我的控制器中,代碼很簡單,看起來像這樣;

$scope.TextToUser='UserText'

在相應的html頁面上,代碼僅為{{TextToUser}}

我試圖通過在控制器中使用以下代碼將文本變成紅色;

$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);
});

現在綁定html

<span ng-bind-html="TextToUser"></span>

您需要包括清理腳本。 https://code.angularjs.org/1.3.2/angular-sanitize.js

注意:-我嘗試使用$interpolate但是如果沒有ngSanitize它會給出有關unsafe內容的錯誤。

工作的朋克在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM