gpt4 book ai didi

angularjs - 如何防止 AngularJS 对 div 进行插值?

转载 作者:行者123 更新时间:2023-12-03 06:56:29 24 4
gpt4 key购买 nike

有没有办法阻止 div 上的插值?我想从中获取原始 html 并通过注入(inject) $interpolate 手动插值,如下所示:

$scope.report = $interpolate($("#myDiv").html())($scope);

最佳答案

使用ngNonBindable指示。摘自文档:

<div>Normal: {{1 + 2}}</div> // => Normal: 3
<div ng-non-bindable>Ignored: {{1 + 2}}</div> // => Ignored: {{1+2}}

但是,请注意,这可能不会编译该元素中存在的其他指令。


另一个选项(我认为这是最正确的)是创建一个指令并在编译阶段捕获元素的内容,然后仅在需要时对其进行插值:

app.directive("myDirective", function( $interpolate ) {
return {
compile: function( tElement ) {
var raw = tElement.html();
return function( scope, element ) {
var interpolated = $interpolate( raw )( scope );
// do something with it
};
}
};
});

关于angularjs - 如何防止 AngularJS 对 div 进行插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25917376/

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