Angular - 编译指令后无法访问控制器方法

Angular - Can't access controller methods after compiling directive

本文关键字:访问 访问控制 控制器 方法 编译 指令 Angular      更新时间:2023-09-26

我正在大量修改所见即所得的js插件,并将我自己的自定义元素插入其中。为了插入自定义元素,我正在使用指令,以便在需要进行更改时可以轻松维护它们。以下是到目前为止我的代码示例:

所见即所得编辑器的初始加载:

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

以下是我插入到所见即所得内容的自定义元素(指令)的示例:

<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);

它按照我的需要编译指令,但棘手的部分来了。我需要能够从外部角度调用"所见即所得的Ctrl"中的函数。我可以在编译之前执行此操作,但由于某种原因,在使用 angular 的编译函数后,我无法访问元素上的范围。

这是在$compile之前工作的代码:

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

$compile后尝试调用 wysiwygModal 函数后出现以下错误:

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

我做错了什么?

我能够在编译的元素上访问scope()及其变量。 如果这没有帮助,您可以编辑此 plunkr 或创建自己的吗?

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

脚本.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)
        }
      });
    }    
  };
});

索引.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>