2016-05-19 26 views
0

NG模糊,NG-焦點事件正在當我使用這個指令時不能綁定ng-change?

然而,NG-變化沒有工作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); 
       } 
      } 
     }; 
    }) 
+0

你是什麼意思「** ng-change **不適用於** contenteditable div **」?它在[JSFiddle上的DEMO](https://jsfiddle.net/ovr9uLur/)上正常工作。 – georgeawg

回答

0

NG-變化僅適用於可與div使用input<select><textarea>元素。

參考此鏈接w3schools

語法

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

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

相關問題