Javascript AngularJS v1.4.2 ng bind html和ng bind html不安全的don';行不通
我使用的是AngularJS v1.4.2,喜欢从Javascript AngularJS v1.4.2 ng bind html和ng bind html不安全的don';行不通,javascript,angularjs,ng-bind-html,Javascript,Angularjs,Ng Bind Html,我使用的是AngularJS v1.4.2,喜欢从$scope中的变量打印html 我尝试使用ng bind html和ng bind html unsafe,但两者都不起作用 这是我的控制器: var ctrl = angular.module('app.ctrl',['app.model']) .controller('Controller',function($scope, $log, Model){ $log.warn("controller fü
$scope
中的变量打印html
我尝试使用ng bind html
和ng bind html unsafe
,但两者都不起作用
这是我的控制器:
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>";
});
var ctrl=angular.module('app.ctrl',['app.model']))
.controller('controller',函数($scope,$log,Model){
$log.warn(“控制器für视图wurde Erstellet”);
$log.log(Model.getValue());
$scope.sayHello=“你好,世界!”;
$scope.sayHelloHtml=“你好,世界费特”;
});
和我的HTML代码:
...
<div ng-controller="Controller">Meine erste angularJS View {{ sayHello }}
<div ng-bind="sayHello"></div>
<div ng-bind-html="sayHelloHtml"></div>
</div>
...
。。。
Meine erste angularJS视图{{sayHello}
...
var ctrl=angular.module('app.ctrl',['app.model','ngSanitize']))
.controller('controller',函数($scope,$log,Model,$sce')){
$log.warn(“控制器für视图wurde Erstellet”);
$log.log(Model.getValue());
$scope.sayHello=“你好,世界!”;
$scope.sayHelloHtml=“你好,世界费特”;
$scope.sayHelloHtml=$sce.trustAsHtml($scope.sayHelloHtml);
});
HTML
Meine erste angularJS视图{{sayHello}
确保在应用程序中包含angular sanitize.js和injectedngSanitize模块,并在控制器中注入$sce。有必要使用$sce()
$scope.sayHelloHtml=$sce.trustAsHtml(“Hello World Fett” 根据ng bind html的文档,您需要将ngSanitize添加到您的模块依赖项中,以使其正常工作,而您似乎没有;) 有关更多详细信息,请查看上的示例 干杯
D“不工作”?您是否收到任何错误?必须信任变量中的HTML内容,否则HTML将被剥离。使用
$sce
信任您的HTML内容。看见
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);
});
<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>