Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs:如何将函数传递到编译_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript Angularjs:如何将函数传递到编译

Javascript Angularjs:如何将函数传递到编译,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有以下指示: app.directive('pagedownAdmin', ['$compile', '$timeout', function ($compile, $timeout) { var nextId = 0; var converter = Markdown.getSanitizingConverter(); converter.hooks.chain("preBlockGamut", function (text, rbg) { retur

我有以下指示:

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();
        }
    }
}]);
app.directive('pagedownAdmin',['$compile','$timeout',函数($compile,$timeout){
var-nextId=0;
var converter=Markdown.getSanitizingConverter();
converter.hooks.chain(“preBlockGamut”,函数(文本,rbg){
返回文本。替换(/^{0,3}”“”*\n((?:.*?\n)+?){0,3}”“*$/gm,函数(整个,内部){
返回“+rbg(内部)+”\n”;
});
});
返回{
限制:“E”,
范围:{
内容:“=”,
模态:'=模态'
},
模板:“”,
链接:功能(范围、元素、属性){
var-editorUniqueId;
如果(attrs.id==null){
editorUniqueId=nextId++;
}否则{
editorUniqueId=attrs.id;
}
scope.hideDiv=函数(){
document.getElementById(“wmd按钮栏-”+editorUniqueId).style.display='none';
};
scope.showDiv=函数(){
document.getElementById(“wmd按钮栏-”+editorUniqueId).style.display='block';
};
范围
var newElement=$compile(
'' +
'' +
'' +
'' +
'' +
'' +
‘测试组’+
“(范围)
;
iElement.append(新元素);
var help=angular.isFunction(scope.help)?scope.help:function(){
//默认情况下重定向到指南
$window.open(“http://daringfireball.net/projects/markdown/syntax“,”空白“);
};
变量编辑器=新的Markdown.editor(转换器“-”+editorUniqueId{
接待员:救命
});
var editorElement=angular.element(document.getElementById(“wmd输入-”+editorUniqueId));
editor.hooks.chain(“onPreviewRefresh”,函数(){
//连接由用户与pagedown控件交互引起的更改
//并在美元内申请
$timeout(函数(){
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>
未捕获引用错误:未定义on 未捕获引用错误:未定义关闭 //第二次尝试 当我点击进出文本区域时,什么都没有发生。没有错误,但也没有调用函数


谁能给我指一下正确的方向吗?感谢您不要使用相同的作用域,而是实例化一个新的作用域(
scope.$new()
)并将函数分配给这个新的作用域。因为否则,您将重写由范围声明分配给范围对象的函数引用

var newScope = scope.$new();
newScope.hideDiv = function() {...};
newScope.showDiv = function() {...};
...
var newElement = $compile(...)(newScope);
要使用(指令的)原始作用域的函数引用,可以调用新作用域函数中的函数引用(例如
function(){scope.hideDiv();}

工作plnkr:


谢谢你们的帮助。我已经发现我的代码有什么问题。我犯了一个非常愚蠢的错误。我分别使用了聚焦
而不是聚焦
ng focus
和模糊
而不是模糊
ng blur

使用聚焦
ng focus
和模糊
ng blur
而不是聚焦
on focus
和模糊
on blur
不使用
new
作为变量名,这是一个特殊的,你不能用它作为var@Michelemvar名称“new”仅用于此问题:)您应该在问题中更正它。顺便说一句,完整的指令代码或更好的JSFIDLE可能有助于找到解决方案。我没有看到任何绑定到您的指令的
pagedownAdmin
元素-您确切地说什么时候使用您的指令?您可能想将此作为编辑添加到您的问题中,如果@Rouby已经回答了您的问号,则将其作为答案。