gpt4 book ai didi

javascript - AngularJS v1.4.2 ng-bind-html 和 ng-bind-html-unsafe 不工作

转载 作者:行者123 更新时间:2023-11-30 12:19:57 27 4
gpt4 key购买 nike

我正在使用 AngularJS v1.4.2 并喜欢从 $scope 中的变量打印出 html。

我尝试使用 ng-bind-htmlng-bind-html-unsafe 但两者都不起作用。

这是我的 Controller :

var ctrl = angular.module('app.ctrl',['app.model'])
.controller('Controller',function($scope, $log, Model){
$log.warn("controller für View wurde erstellt");
$log.log(Model.getValue());
$scope.sayHello="Hello World!";
$scope.sayHelloHtml="<strong>Hello World Fett</strong>";
});

还有我的 HTML 代码:

...
<div ng-controller="Controller">Meine erste angularJS View {{ sayHello }}
<div ng-bind="sayHello"></div>
<div ng-bind-html="sayHelloHtml"></div>
</div>
...

最佳答案

var ctrl = angular.module('app.ctrl',['app.model','ngSanitize'])
.controller('Controller',function($scope, $log, Model,'$sce'){
$log.warn("controller für View wurde erstellt");
$log.log(Model.getValue());
$scope.sayHello="Hello World!";
$scope.sayHelloHtml="<strong>Hello World Fett</strong>";

$scope.sayHelloHtml = $sce.trustAsHtml($scope.sayHelloHtml);
});

HTML

 <div ng-controller="Controller">Meine erste angularJS View {{ sayHello }}
<div ng-bind="sayHello"></div>
<div ng-bind-html="sayHelloHtml"></div>
<div ng-bind-html-unsafe="sayHello"></div>
</div>


确保您已在应用中包含 angular-sanitize.js 并注入(inject) ngSanitize 模块,并在您的 Controller 中注入(inject) $sce

关于javascript - AngularJS v1.4.2 ng-bind-html 和 ng-bind-html-unsafe 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430581/

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