【问题标题】:Angularjs: How to pass function into compileAngularjs:如何将函数传递给编译
【发布时间】:2015-07-04 07:21:03
【问题描述】:

我有以下指令:

app.directive('pagedownAdmin', ['$compile', '$timeout', function ($compile, $timeout) {
    var nextId = 0;
    var converter = Markdown.getSanitizingConverter();
    converter.hooks.chain("preBlockGamut", function (text, rbg) {
        return text.replace(/^ {0,3}""" *\n((?:.*?\n)+?) {0,3}""" *$/gm, function (whole, inner) {
            return "<blockquote>" + rbg(inner) + "</blockquote>\n";
        });
    });

    return {
        restrict: "E",
        scope: {
            content: "=",
            modal: '=modal'
        },
        template: '<div class="pagedown-bootstrap-editor"></div>',
        link: function (scope, iElement, attrs) {

            var editorUniqueId;

            if (attrs.id == null) {
                editorUniqueId = nextId++;
            } else {
                editorUniqueId = attrs.id;
            }

            scope.hideDiv = function () {
                document.getElementById("wmd-button-bar-" + editorUniqueId).style.display = 'none';
            };

            scope.showDiv = function () {
                document.getElementById("wmd-button-bar-" + editorUniqueId).style.display = 'block';
            };

            scope;

            var newElement = $compile(
                '<div>' +
                    '<div class="wmd-panel">' +
                        '<div data-ng-hide="modal.wmdPreview == true" id="wmd-button-bar-' + editorUniqueId + '" style="display:none;"></div>' +
                            '<textarea on-focus="showDiv()" on-blur="hideDiv()" data-ng-hide="modal.wmdPreview == true" class="wmd-input" id="wmd-input-' + editorUniqueId + '" ng-model="content" >' +
                            '</textarea>' +
                        '</div>' +
                    '<div data-ng-show="modal.wmdPreview == true" id="wmd-preview-' + editorUniqueId + '" class="pagedownPreview wmd-panel wmd-preview">test div</div>' +
                '</div>')(scope)
            ;

            iElement.append(newElement);

            var help = angular.isFunction(scope.help) ? scope.help : function () {
                // redirect to the guide by default
                $window.open("http://daringfireball.net/projects/markdown/syntax", "_blank");
            };

            var editor = new Markdown.Editor(converter, "-" + editorUniqueId, {
                handler: help
            });

            var editorElement = angular.element(document.getElementById("wmd-input-" + editorUniqueId));

            editor.hooks.chain("onPreviewRefresh", function () {
                // wire up changes caused by user interaction with the pagedown controls
                // and do within $apply
                $timeout(function () {
                    scope.content = editorElement.val();
                });
            });

            editor.run();
        }
    }
}]);

在里面我有 showDiv 和 hideDiv 函数,当我点击进入和退出文本区域时,它们会显示和隐藏页面编辑器的菜单。

我将函数传递给编译内的事件:

//First try
<textarea onfocus="showDiv()" onblur="hideDiv()"></textarea>

当我在文本区域的内部和外部单击时,出现错误:

Uncaught ReferenceError: on is not defined
Uncaught ReferenceError: off is not defined

//Second try
<textarea on-focus="showDiv()" on-blur="hideDiv()"></textarea>

当我点击进入和退出 textarea 时,什么也没有发生。没有错误但也没有调用函数。

谁能指出我正确的方向?谢谢

【问题讨论】:

  • 分别使用ng-focusng-blur代替onfocusonblur
  • 不要使用new作为变量名,它是一个特殊的,不能作为var使用
  • @Michelem var name 'new' 只是为了这个问题:)
  • 您应该在问题中更正它。顺便说一句,完整的指令代码或更好的 JSFiddle 可能有助于找到解决方案。
  • 我没有看到任何pagedownAdmin 元素绑定到你的指令 - 你什么时候使用你的指令?

标签: javascript angularjs angularjs-directive


【解决方案1】:

不要使用相同的作用域,而是实例化一个新作用域 (scope.$new()) 并将函数分配给这个新作用域。因为否则您将覆盖范围声明分配给范围对象的函数引用。

var newScope = scope.$new();
newScope.hideDiv = function() {...};
newScope.showDiv = function() {...};
...
var newElement = $compile(...)(newScope);

并且要使用赋予原始作用域(指令的)的函数引用,您可以在新作用域函数中调用这些函数引用(例如function() { scope.hideDiv(); })。

工作 plnkr:

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

https://docs.angularjs.org/api/ng/service/$compile https://code.angularjs.org/1.3.16/docs/api/ng/type/$rootScope.Scope#$new

【讨论】:

    【解决方案2】:

    感谢你们试图提供帮助。我发现我的代码有什么问题。我犯了一个非常愚蠢/菜鸟的错误。我用on-focus 代替ng-focuson-blur 代替ng-blur

    【讨论】:

    • 您可能希望将此作为编辑添加到您的问题中,如果@Rouby 回答了您的问号,则作为答案。
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多