5

私はちょうど Angularjs を理解し始めたところですが、ドキュメントで以下を見てきました。ng-bind-html と ng-model の両方を一緒に使用すると競合すると思いますか?

angular.module('customControl', []).
  directive('contenteditable', function() {
    return {
      restrict: 'A', // only activate on element attribute
      require: '?ngModel', // get a hold of NgModelController
      link: function(scope, element, attrs, ngModel) {
        if(!ngModel) return; // do nothing if no ng-model

        // Specify how UI should be updated
        ngModel.$render = function() {
          element.html(ngModel.$viewValue || '');
        };

        // Listen for change events to enable binding
        element.on('blur keyup change', function() {
          scope.$apply(read);
        });
        read(); // initialize

        // Write data to the model
        function read() {
          var html = element.html();
          // When we clear the content editable the browser leaves a <br> behind
          // If strip-br attribute is provided then we strip this out
          if( attrs.stripBr && html == '<br>' ) {
            html = '';
          }
          ngModel.$setViewValue(html);
        }
      }
    };
  });

https://docs.angularjs.org/api/ng/type/ngModel.NgModelControllerから

現在、以下のように ng-bind-html ディレクティブを使用しています (双方向バインディングではありませんが、うまく機能します)。

<div ng-bind-html="person.nameHtml" contenteditable="true"></div>
4

1 に答える 1

2

質問に対するコメントによると、答えは次のngModelディレクティブを使用できるということです。

<div ng-bind-html="person.nameHtml"
     contenteditable="true"
     ng-model="person.nameHtml">
</div>
于 2014-07-31T03:14:30.160 に答える