【问题标题】:Angular - Can't access controller methods after compiling directiveAngular - 编译指令后无法访问控制器方法
【发布时间】:2013-10-30 06:07:19
【问题描述】:

我正在大量修改所见即所得的 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);

它会按照我的需要编译指令,但棘手的部分来了。我需要能够从 OUTSIDE 角度调用 'wysiwygCtrl' 内的函数。我可以在编译之前执行此操作,但由于某种原因,在使用 angular 的编译功能后,我无法访问元素的范围。

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

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

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

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

我做错了什么?

【问题讨论】:

    标签: javascript angularjs wysiwyg


    【解决方案1】:

    我能够访问scope() 及其在已编译元素上的变量。如果这没有帮助,您可以编辑此 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-14
      • 2015-06-28
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2016-03-18
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多