我想删除一些带入DOM的元素......
<div ng-bind-html-unsafe="whatever"></div>
我编写了一个删除元素的函数,但我需要一种在ng-bind-html-unsafe
完成后触发函数的方法。是否有ng-bind-html-unsafe
的回调或更好的方法?
答案 0 :(得分:1)
ng-bind-html-unsafe已从角度的当前版本(1.2+)中删除。我建议在新版本的angular中使用$ sanitize服务。您需要包含清理库并将其添加到模块中。
通过这种方式,您可以在完成清理操作后执行任何操作,而不必担心回调。
快速而肮脏的实施:
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js"></script>
<script src="libs/angular/angular-sanitize.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<div>{{sanitized}}</div>
</div>
<script>
angular.module('myApp', ['ngSanitize'])
.controller('myController', ['$scope', '$sanitize', function($scope, $sanitize) {
$scope.sanitized = $sanitize('someTextFromSomeSource');
// Do whatever you want here with the cleaned up text
}])
</script>
</body>
</html>
答案 1 :(得分:0)
我会将您演示中的html移动到directive
:
<div ng-bind-html-unsafe="whatever"></div>
在指令中我会根据我的需要操作html,我在这里基于一个假设,因为我不确定whatever
变量的更新频率或方式,因此通用解决方案将是{ {1}}监听这个变量,如下所示:
$watch
我的新指令中的所有代码,可能您都希望使用$scope.$watch('whatever',function(newValue,oldValue, scope){
//do something
});
函数
以下是the Documentation from Angular。
后连接功能 链接子元素后执行。在链接后功能中进行DOM转换是安全的。