AngularJS NG绑定HTML的2路数据绑定 [英] AngularJS ng-bind-html 2way data binding
本文介绍了AngularJS NG绑定HTML的2路数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个AngularJS应用,在那里我从一个Web服务得到了一些数据和分析一些HTML与NG绑定,HTML ...模板,但是当我尝试绑定NG绑定,HTML中的数据 - 什么也没有发生。 。任何人吗?
I have an AngularJS app where I got some data from a webservice and parse some HTML to the template with ng-bind-html ... but when I try to bind data inside the ng-bind-html - nothing happens .. anyone?
我有一个小例子在这里..不正确的情况下。
I have a little example here,.. not the right case.
HTML
<div ng-controller="MyCtrl">
<div ng-bind-html="post"></div>
</div>
的JavaScript
Javascript
angular.module('myApp',[])
.controller('MyCtrl', function($scope, $sce) {
$scope.name = 'World';
$scope.post = $sce.trustAsHtml("<h1>hello {{name}}</h1>");
});
推荐答案
DEMO
添加此指令
angular.module("myApp").directive('compileTemplate', ["$compile", "$parse", function($compile, $parse) {
return {
restrict: 'A',
link: function($scope, element, attr) {
var parse = $parse(attr.ngBindHtml);
function value() { return (parse($scope) || '').toString(); }
$scope.$watch(value, function() {
$compile(element, null, -9999)($scope);
});
}
}
}]);
这篇关于AngularJS NG绑定HTML的2路数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文