使用此指令时无法绑定ng-change?

时间:2016-05-19 07:09:49

标签: javascript angularjs ionic-framework ionic-view

ng-blur,ng-focus事件正在运作

但是, ng-change 不适用于 contenteditable div

我已将div更改为可编辑,这必须像textarea一样做出反应。

所以我用了contenteditable div。任何人都可以帮我找一个有效的解决方案吗?

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(readViewText);
              });

              // No need to initialize, AngularJS will initialize the text based on ng-model attribute

              // Write data to the model
              function readViewText() {
                  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);
              }
          }
      };
  })

1 个答案:

答案 0 :(得分:0)

ng-change仅适用于可与input一起使用的<select><textarea>div元素。

请参阅此链接w3schools

<强>语法

<element ng-change="expression"></element>

<input><select><textarea>支持。