2

WYSIWYG js プラグインを大幅に変更しており、独自のカスタム要素を挿入しています。カスタム要素を挿入するためにディレクティブを使用しているため、変更が必要な場合に簡単に維持できます。これまでの私のコードの例は次のとおりです。

wysiwyg エディターの初期ロード:

<div ng-controller="wyswiygCtrl">
    <textarea wysiwyg-editor ng-model="content"></textarea>
</div>

以下は、wysiwyg コンテンツに挿入するカスタム要素 (ディレクティブ) の例です。

<wysiwyg-element class="custom_element" name="awesome" type="checkbox" checked="true"></wysiwyg-element>

ディレクティブの初期化内で次のコードを使用して、その中のカスタム要素 (ディレクティブ) をコンパイルしています。

var e = angular.element(wysiwygEditor.get(0).innerHTML);
$compile(e.contents())(scope);
wysiwygEditor.html(e);

ディレクティブは必要に応じてコンパイルされますが、ここで注意が必要です。OUTSIDE angular から「wysiwygCtrl」内で関数を呼び出せるようにする必要があります。コンパイル前にこれを行うことはできますが、何らかの理由で、角度のコンパイル機能を使用した後、要素のスコープにアクセスできません。

の前に機能するコードは次の$compileとおりです。

angular.element($('.custom_element')).scope().wysiwygModal();
angular.element($('.custom_element')).scope().$apply();

wysiwygModal$compile の後に関数を呼び出そうとすると、次のエラーが発生します。

Uncaught TypeError: Object #<Object> has no method 'wysiwygModal'

私は何を間違っていますか?

4

1 に答える 1

1

scope()コンパイルされた要素の とその変数にアクセスできました。これで問題が解決しない場合は、この plunkr を編集するか、独自の plunkr を作成していただけますか?

http://plnkr.co/edit/kvbvKXeVjEhmeE7257ly?p=preview

script.js

var myApp = angular.module('myApp', []);

myApp.directive('myEditor', function () {
  return {
    restrict: 'E',
    scope: {
    },
    template: 
      '<div>' + 
        '<h3>Raw HTML</h3>' + 
          '<textarea rows=5 cols=40 ng-model="text"></textarea>' + 
        '</div>' + 
        '<hr />' +
        '<div>' + 
          '<h3>Compiled HTML</h3>' + 
          '<div my-preview></div>' + 
        '</div>' +  
        '<hr />' +
        '<div>' + 
          '<h3>Messages generated during compilation</h3>' + 
          '<div ng-repeat="message in messages">[{{message}}]</div>' + 
        '</div>' + 

      '</div>',
    controller: function ($scope) {
      $scope.messages = [];

      this.getText = function () {
        return $scope.text;
      };

      this.addMessage = function (message) {
        $scope.messages.push(message);
      };

      this.clearMessages = function () {
        $scope.messages.length = 0;
      };
    },
    link: function (scope, element, attrs) {
      scope.text = '<div ng-init="a = 2" class="my-selector">\n    scope.a : [{{a}}]\n</div>';
    }
  };
});

myApp.directive('myPreview', function ($compile) {
  return {
    require: '^myEditor',
    link: function (scope, element, attrs, myEditorController) {
      scope.$watch(myEditorController.getText, function (newValue) {
        if (newValue !== undefined) {
          var e = angular.element('<div>' + newValue + '</div>');
          $compile(e)(scope);
          element.html(e);

          myEditorController.addMessage(
            'The value of "a" on the scope of the compiled element is: ' + 
            angular.element($('.my-selector')).scope().a)
        }
      });
    }    
  };
});

index.html

<!DOCTYPE html>
<html>

  <head>
    <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>

    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="myApp">
      <my-editor></my-editor>
    </div>
  </body>

</html>
于 2013-10-31T23:35:34.977 に答える