1

我正在使用textangular作为我正在从事的项目的富文本解决方案。

需要清理此输入,因为我们只允许某些 html 标记。

由于这不是 textangular 的默认可能性,我创建了一个包含 textangular 指令的指令。我可以成功清理输入文本,但视图永远不会更新,我已经没有想法如何实现这一点

指示:

.directive('chTextAngular',  function(){

    return {
      restrict: 'E',
      require: 'ngModel',
      template: function(elements, attributes) {
        return '<div text-angular ng-model="' + attributes.ngModel + '" id="' + attributes.id + '"></div>';
      },
      link: function(scope, element, attributes, ngModel) {
        scope.$watch(attributes.ngModel, function(n, o) {
          if(n !== undefined) {

            // Replace all the divs with <br> tags
            var sanitized = ngModel.$viewValue.replace(/<div>/gm, '<br>').replace(/<\/div>/gm, ' ');
            sanitized = sanitized.replace(/<p>/gm, '').replace(/<\/p>/gm, '<br><br>');

            sanitized = $.htmlClean(sanitized, {
              allowedTags: ["strong", "em", "ul", "ol", "li", "a", "b", "i", "br"],
              allowedAttributes: ["a", "href"]
            });

            console.log(sanitized);

            ngModel.$setViewValue(sanitized);

          }
        });
      }
    }
});

日志打印出模型,它表明它实际上是变化的。我只是不知道如何在 textangular 中更新实际的 textarea。

任何人都可以帮我解决这个问题,或者让我朝着正确的方向前进吗?

4

1 回答 1

1

好的,我要问的第一个问题是;如果模型是正确的数据,您真的需要更新视图吗?

其次,您可能应该通过注册您的功能ngModel.$parsers.push(function(n,o){...})以避免额外的观察者。

它不更新的原因是我们在 textAngular 中有一个问题,以防止模型在有人打字时更新视图。如果您在字段集中时执行此操作,那么您会遇到光标移回文本字段的开头/结尾的问题。如果您确实想从模型更新视图,则注册一个模糊事件处理程序 ( element.on('blur', ...);) 并且调用scope.updateTaBindtaTextElement()可能会根据您的指令附加到的范围而起作用。如果该功能不起作用,则必须将 name 属性添加到 textAngular 元素,然后使用该textAngularManager.refreshEditor(name);功能。

于 2014-09-21T23:00:30.323 回答