Javascript 可以在从服务检索的HTML中使用角度绑定表达式吗?
我有一个html片段,它来自angular服务检索的mongo db。然后,使用ngSanitize中的Javascript 可以在从服务检索的HTML中使用角度绑定表达式吗?,javascript,angularjs,html-sanitizing,Javascript,Angularjs,Html Sanitizing,我有一个html片段,它来自angular服务检索的mongo db。然后,使用ngSanitize中的ngbindhtml指令在我的页面上清理此html片段。我想知道如何将html代码段内的表达式绑定到页面,以便在绑定html时,绑定表达式在代码段内执行。例如,下面是我的div,我的html代码段将绑定到该div: <div ng-bind-html="middle_snippet"> </div> 因
ngbindhtml
指令在我的页面上清理此html片段。我想知道如何将html代码段内的表达式绑定到页面,以便在绑定html时,绑定表达式在代码段内执行。例如,下面是我的div,我的html代码段将绑定到该div:
<div ng-bind-html="middle_snippet">
</div>
因此,我的预期结果将是html:
为什么是赫尔罗!!!装订得很好
然而,我得到了带有花括号的原始html。任何想法都会有帮助,因为我在这里站着不动,试图让这个工作
更新:在阅读了关于编译服务的内容并将其与指令一起使用之后,正如Chandermani所提到的,我既开悟又困惑。AngularJS团队认为,使用$compile时的“最佳实践”方法是在此处所述的指令定义对象(DDO)中使用$compile。好的,我创建了一个DDO,但是,我对DDO的compile:method感到困惑。以下是我到目前为止的情况:
var myModule = angular.module('html_compile');
myModule.directive('html_compiler', function factory(injectables) {
var directiveDefinitionObject = {
priority: 0,
template: $scope.snippet,
replace: true,
transclude: false,
restrict: 'E',
scope: false,
compile: function compile(tElement, tAttrs) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
// or
// return function postLink( ... ) { ... }
},
// or
// link: {
// pre: function preLink(scope, iElement, iAttrs, controller) { ... },
// post: function postLink(scope, iElement, iAttrs, controller) { ... }
// }
// or
// link: function postLink( ... ) { ... }
};
return directiveDefinitionObject;
});
因此@Chandermani用
$compile
服务击中了它的头部,当然这是注释中链接的页面末尾的示例,但是,这有点令人困惑,因为在同一页面上列出的指令定义对象中有一个“compile”元素。因此,我猜这个compile元素决定了您要如何以及何时编译您在DDO中创建的指令,底部的示例涉及$compileProvider
,并根据它创建了一个指令。有完整解释答案的博客位于此处:您需要使用$compile服务编译您收到的内容并将其注入html。因此我也需要为此创建自定义指令,对吗?请看$compile文档,最后我开始阅读一个关于compile的compile指令,有没有一种方法可以让ui路由器在嵌套视图中即时完成这项工作?使用ui视图
html标记包含的任何内容都会在ui视图中自动编译。
var middleMainContent = $Content.content({slug: "home-bottom-middle"
}, function () {
console.log(JSON.stringify(middleMainContent));
$scope.middle_snippet = middleMainContent.response.content.snippet;
$scope.myBinding = 'VERY NICE BINDING';
});
var myModule = angular.module('html_compile');
myModule.directive('html_compiler', function factory(injectables) {
var directiveDefinitionObject = {
priority: 0,
template: $scope.snippet,
replace: true,
transclude: false,
restrict: 'E',
scope: false,
compile: function compile(tElement, tAttrs) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
// or
// return function postLink( ... ) { ... }
},
// or
// link: {
// pre: function preLink(scope, iElement, iAttrs, controller) { ... },
// post: function postLink(scope, iElement, iAttrs, controller) { ... }
// }
// or
// link: function postLink( ... ) { ... }
};
return directiveDefinitionObject;
});