angularサービスによって取得されたmongo dbからのhtmlスニペットがあります。この HTML スニペットは、ng-bind-html
ngSanitize のディレクティブを使用してページでサニタイズされます。HTMLスニペット内の式をページにバインドする方法を知りたいので、htmlをバインドすると、バインディング式がスニペット内で実行されます。たとえば、html スニペットがバインドされる div は次のとおりです。
<div ng-bind-html="middle_snippet">
</div>
これがサービスからプルされる実際のスニペットです。
<p>WHY HERRRRO!!! {{myBinding}}</p>
そして、私の単純なコントローラーは次のとおりです。
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';
});
したがって、実際に期待される結果は html になります。
なぜヘロ!!! とても素敵なバインディング
ただし、中括弧を使用して元の html を取得します。私はこれを機能させようとしてここに立っているので、どんなアイデアでも役に立ちます。
更新: したがって、チャンデルマニが述べたように、コンパイル サービスについて読み、それらをディレクティブと共に使用した後、私は啓発され、混乱しています。$compile を使用するときの AngularJS チームによる「ベスト プラクティス」の方法は、http://docs.angularjs.org/api/ng/service/ $compile に記載されているように、ディレクティブ定義オブジェクト (DDO) 内で使用することです。わかりましたので、DDO を作成しましたが、DDO の compile: メソッドに何が入るのか混乱しています。これが私がこれまでに持っているものです:
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;
});