angularjs 如何在“title”标签内调用这个“ng-bind-html”?

toiithl6  于 2022-10-31  发布在  Angular
关注(0)|答案(2)|浏览(133)

我尝试在“title“标记内使用“ng-bind-html“指令来呈现带有货币符号的货币值,但表达式未求值。
这就是我的特灵:

$scope.salary= 10000;
$scope.currencySymbol = '€';//€
<div title="{{ng-bind-html= 'salary |currency:(currencySymbol+' '): 0'}}"> //This is not working
<span ng-bind-html="salary |currency:(currencySymbol+' '): 0"> </span> //working fine and renders €10000

当我在<span>标记内显示时,它工作正常,但我也想在<title>标记中显示相同的格式化值。
因此,想知道如何在“title“标记中调用此“ng-bind-html“,以显示与在<span>标记中相同的输出?

0md85ypi

0md85ypi1#

ng-bind-html is a directive。大括号用于表达式。将不会从表达式中处理指令。
按如下所示重写表达式:

<div title="{{ salary | currency:currencySymbol: 0}}"></div>

并将您的currencySymbol值替换为货币前缀所需的字符串。$scope.currencySymbol = "€ ";
Here is a working plunkr
Additional documentation on the currency filter
Related documentation on expressions
如果你真的专注于动态处理货币符号并将其视为html,你将需要编写一个接受sceHtmlString的自定义货币过滤器。Javascript字符串支持unicode,所以你提供的用例似乎不需要这样做。

qmb5sa22

qmb5sa222#

试试这个你可以添加标题到div。但它将是不可见的,因为标题不是一个文本字段。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-sanitize.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<div ng-bind-html="myText"></div>
 <span>Currency Example:</span>
      <div> {{ salary | currency:currencySymbol: 0}} </div>
       <div title=" {{ salary | currency:currencySymbol: 0}}"> nodata</div>
     <span>Currency Example ng-bindhtml</span>
     <span ng-bind-html="myText"></span>
     <p ng-bind-html="myText"></p>
    </div>
</div>

<script>
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
    $scope.myText = "My name is: <h1>  111 John Doe</h1>";
     $scope.salary = 10000;
   $scope.salaryhtml = "<p> €10000 </p>";
  $scope.currencySymbol = "€";
  $scope.myText ="  <h1>$ 1000 </h1>";
});
</script>

</body>
</html>

相关问题