gpt4 book ai didi

javascript - 如果父标签包含 ng-bind-html 指令,则没有 html 标签起作用

转载 作者:可可西里 更新时间:2023-11-01 13:28:44 25 4
gpt4 key购买 nike

以下是我的代码示例,如果我删除了 ng-bind-html 那么 div 正在工作,如果没有失败,请给我一个答案

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js"></script>

<script>
var app = angular.module('myApp', ['ngSanitize']);

app.controller('CartController', function($scope) {
$scope.title = "<p>hello world</p>";
$scope.test = "How are you..!How are you..!How are you..!";
});
</script>

<body ng-app="myApp" >
<div ng-controller="CartController">
<label ng-bind-html=title>
<div>{{test}}</div>
</label>
</div>
</body>
</html>

最佳答案

您需要使用 $sce.trustAsHtml$sce 注入(inject) Controller :

JSFiddle

app.controller('CartController', function($scope, $sce) {
$scope.title = $sce.trustAsHtml("<p>hello world</p>");
$scope.test = "How are you..!How are you..!How are you..!";
});

当然,HTML label 将完全被 $scope.title 取代,您将看不到 {{test}} 不再是

关于javascript - 如果父标签包含 ng-bind-html 指令,则没有 html 标签起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33567927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com