19 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) {
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();
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
在里面我有showDiv和hideDiv函数,当我点击进出textarea时,它会显示和隐藏页面编辑器的菜单.
我将函数传递给编译中的事件:
//First try
<textarea onfocus="showDiv()" onblur="hideDiv()"></textarea>
Run Code Online (Sandbox Code Playgroud)
当我点击textarea内外时,我得到错误:
Uncaught ReferenceError: on is not defined
Uncaught ReferenceError: off is not defined
//Second try
<textarea on-focus="showDiv()" on-blur="hideDiv()"></textarea>
Run Code Online (Sandbox Code Playgroud)
当我点击进出textarea时,没有任何事情发生.没有错误,但也没有调用函数.
有人能指出我正确的方向吗?谢谢
Rou*_*uby 12
而不是使用相同的范围,实例化新的范围(scope.$new())并将函数分配给此新范围.因为否则您将覆盖scope-declaration为scope-object指定的函数引用.
var newScope = scope.$new();
newScope.hideDiv = function() {...};
newScope.showDiv = function() {...};
...
var newElement = $compile(...)(newScope);
Run Code Online (Sandbox Code Playgroud)
并且要使用给定原始范围(指令)的函数引用,可以在新范围函数中调用它们(例如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-focus和on-blur代替ng-blur。
| 归档时间: |
|
| 查看次数: |
808 次 |
| 最近记录: |