AngularJs学习html转义

MainApp.directive('ngHtml', function () {
        function watch(scope, el, watchExp){
            scope.$watch(watchExp , function(html){
                if(el.data("replace")){
                    el.after(html || '');
                    el.remove();
                }
                else {
                    el.html(html||"");
                }
            });
        }
    
        return function(scope , el , attr){
            var innerText = $.trim(el.text());
            if(/^\{\{.*\}\}$/.test(innerText)){
                var watchExp = innerText.replace(/^\{\{(.*)\}\}$/, '$1');
                watch(scope, el, watchExp);
            }
            else if(attr.value){
                watch(scope, el, attr.value);
            }
        };
});

 

<ng-html >{{html}}</ng-html>
<ng-html value="html" data-replace="true" />

 

posted @ 2015-09-22 22:13  rubekid  阅读(180)  评论(0编辑  收藏  举报