更改$compile对象的 html 在 Angular 中失败
Changing the html of a $compile's object fails in Angular
Angular 1.5:我有一个ng-repeat,它渲染的文本中有ng点击的html。工作正常。
这是我的编译指令:
app.controller('PageCtrl', pageController)
.directive('compile', [
'$compile', function($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
return scope.$eval(attrs.compile);
},
function(value) {
element.html(value);
$compile(element.contents())(scope);
}
);
};
}
]);
这是我与编译指令的 html 绑定
<div class="mt10" ng-repeat="row in aqdas.Notes | filter: filterNotes">
<span class="pull-left text-bold mr5">{{row.Number}}. </span>
<div compile ng-bind-html="TrustDangerousSnippet(row.Text,'note')">
{{row.Text}}
</div>
</div>
这是我的TrustDangerousSnippet(简化)
$scope.TrustDangerousSnippet = function(text, kind) {
var simpletext = new RegExp("(" + $scope.search + ")", "gi");
text = text.replace(simpletext, "<mark>$1</mark>");
var val = $sce.trustAsHtml(text);
return val;
};
问题:在 TrustDangerousSnippet 中,我使用 reg ex 来突出显示文本的一个区域。当我这样做时,编译不起作用。似乎当我更改要绑定的文本时,$compile不喜欢/允许它?
我怎样才能让$compile喜欢它,或者以其他方式突出显示文本?
也许是这个?
<div compile="TrustDangerousSnippet(row.Text, 'note')"></div>
指令需要参数。尝试这样并删除手表(执行一次编译),您可能会遇到摘要问题。
app.controller('PageCtrl', pageController)
.directive('compile', [
'$compile', function($compile) {
return function(scope, element, attrs) {
var html = scope.$eval(attrs.compile);
element.append($compile(html)(scope));
};
}
]);
相关文章:
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- 由于angular定义的构造函数,Lodash-isEqual失败
- 对php的Angular post请求失败
- 使用jquery从angular表达式中获取内容失败
- 验证失败时阻止表单提交(Angular Material)
- 为什么Angular 1.5双向绑定在将作用域变量传递给组件绑定时失败
- Angular 2 快速入门<脚本 /> 失败
- Angular Jasmine测试因源缩小而失败
- Angular JS,把json放到ng重复出一个事件失败
- $cacheFactory removeAll() 在服务中的单元测试在 Angular 1.2.0 中失败,但在 1.
- 在 Angular JS 中应用日期失败的过滤器
- Angular JS自定义验证器失败,并显示“ng-invalid-maxlength”
- 更改$compile对象的 html 在 Angular 中失败
- Angularjs 数据绑定失败,Angular UI-Bootstrap typeahead
- Angular ajax 使用 $http 失败
- 失败绑定 Angular 承诺$scope变化
- 节点脚本需要向调用它的 Angular 服务返回成功/失败
- Angular Meteor应用程序在localhost中工作,但在部署到服务器时失败
- Angular--实例化模块失败
- 当我使用数据绑定时,angular地理编码器失败