Angularjs 在我的指令中有一个作用域。$apply(函数()在Angular 1.2.2中被调用,但在Angular 1.2.7中没有调用
我有以下指示:Angularjs 在我的指令中有一个作用域。$apply(函数()在Angular 1.2.2中被调用,但在Angular 1.2.7中没有调用,angularjs,Angularjs,我有以下指示: app.directive('pagedownAdmin', function ($compile) { var nextId = 0; var markdownConverter = new Markdown.Converter(); var converter1 = Markdown.getSanitizingConverter(); converter1.hooks.chain("preBlockGamut", function (text
app.directive('pagedownAdmin', function ($compile) {
var nextId = 0;
var markdownConverter = new Markdown.Converter();
var converter1 = Markdown.getSanitizingConverter();
converter1.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 {
require: 'ngModel',
replace: true,
scope: {
modal: '=modal',
ngModel: '=',
pid: '=pid'
},
template: '<div class="pagedown-bootstrap-editor"></div>',
link: function (scope, element, attrs, ngModel) {
var editorUniqueId;
if (attrs.id == null) {
editorUniqueId = nextId++;
} else {
editorUniqueId = attrs.id;
}
var template = '<div>' +
'<div class="wmd-panel">' +
'<div data-ng-hide="modal.wmdPreview == true" id="wmd-button-bar-' + editorUniqueId + '"></div>' +
'<textarea data-ng-hide="modal.wmdPreview == true" class="wmd-input" id="wmd-input-' + editorUniqueId + '">' +
'</textarea>' +
'</div>' +
'<div data-ng-show="modal.wmdPreview == true" id="wmd-preview-' + editorUniqueId + '" class="pagedown-preview wmd-panel wmd-preview">test div</div>' +
'</div>';
element.html($compile(template)(scope));
var converter = new Markdown.Converter();
var help = function () {
var rawContent = $wmdInput.val();
scope.$apply(function () {
ngModel.$setViewValue(rawContent);
});
}
var editor = new Markdown.Editor(converter1, "-" + editorUniqueId, {
handler: help
});
editor.run();
var $wmdInput = $("#wmd-input-" + editorUniqueId);
$wmdInput.on('keyup', _.debounce(function () {
rawContent = $wmdInput.val(); // LINE 1
scope.$apply(function () {
ngModel.$setViewValue(rawContent); // LINE 2
});
}, 500));
}
}
});
我首先输入字母“q”,然后输入字母“x”,版本为1.2.3:
efore change: ngModel.$modelValue:zzx pagedownAdmin.js:68
Before change: ngModel.$viewlValue:undefined pagedownAdmin.js:69
After change: ngModel.$modelValue:q pagedownAdmin.js:72
After change: ngModel.$viewlValue:undefined pagedownAdmin.js:73
Before change: ngModel.$modelValue:zzx pagedownAdmin.js:68
Before change: ngModel.$viewlValue:undefined pagedownAdmin.js:69
After change: ngModel.$modelValue:x pagedownAdmin.js:72
After change: ngModel.$viewlValue:undefined pagedownAdmin.js:73
似乎每次设置ngModel时,但下一次它进入密钥处理程序时,值都是原始值。如果您检查它是否适用于大于1.2.2的版本,那就太好了,因此我们知道哪个版本破坏了您的代码,我现在将开始检查每个版本。感谢1.2.3转到第2行,但它似乎没有保存任何内容。W当我与Fiddler检查时,返回服务器的数据与rawContent不匹配。它与原始数据相同。如果我进行更改,它会在{}内正确设置ngModel,但下次我进行另一次更改时,会发现ngModel仍保持原始值。angular.js 1.2.3:
此版本中没有中断更改(承诺!)
?!
efore change: ngModel.$modelValue:zzx pagedownAdmin.js:68
Before change: ngModel.$viewlValue:undefined pagedownAdmin.js:69
After change: ngModel.$modelValue:q pagedownAdmin.js:72
After change: ngModel.$viewlValue:undefined pagedownAdmin.js:73
Before change: ngModel.$modelValue:zzx pagedownAdmin.js:68
Before change: ngModel.$viewlValue:undefined pagedownAdmin.js:69
After change: ngModel.$modelValue:x pagedownAdmin.js:72
After change: ngModel.$viewlValue:undefined pagedownAdmin.js:73