2015-11-04 62 views
0

我正在嘗試查看我的tinyMCE是否在寫入時有一個內容,如果是或不是(我使用的是AngularJS),我設置了一個標誌。因此,這是我使用的微型代碼:檢查tinyMCE是否有內容

$scope.tinyHasContent = false; 
tinymce.init({ 
       selector: "#elm1", 
       height: 400, 
       language:'it', 
       plugins: [ 
        "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker", 
        "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking", 
        "save table contextmenu directionality emoticons template paste textcolor" 
       ], 
       content_css: "css/partials/content.css", 
       toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage", 
       style_formats: [ 
        {title: 'Bold text', inline: 'b'}, 
        {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}}, 
        {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}}, 
        {title: 'Example 1', inline: 'span', classes: 'example1'}, 
        {title: 'Example 2', inline: 'span', classes: 'example2'}, 
        {title: 'Table styles'}, 
        {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'} 
       ], 
       setup : function(ed){ 
        ed.on('NodeChange', function(e){ 

         if(ed.getContent() != '' || ed.getContent() != undefined || ed.getContent() != null) { 
          $scope.tinyHasContent = true; 
          console.log('the content ' + ed.getContent() + " " + $scope.tinyHasContent); 
         } else { 
          $scope.tinyHasContent = false; 
          console.log('the content ' + ed.getContent() + " " + $scope.tinyHasContent); 
         } 
        }); 
       } 
      }); 

簡單地說,在打字時,我可以看到我寫的日誌。但問題在於,即使在init中總是有內容,因爲它在日誌中始終返回$ scope.tinyHasContent = true。怎麼可能?

順便說,總之,這是我應該做的:

<div ng-if="tinyHasContent == false"> 
<p>Hello</p> 
</div> 

,但實際上它不工作

回答

0

事情是ed.getContent()回報 - 即使你沒有在任何類型的 - 默認的根塊元素(即段落)。這就是爲什麼tinyHasContent永遠是真的。

要驗證這一點,您可能需要將ed.getContent()打印到您的控制檯。