gpt4 book ai didi

angularjs - 如何在自定义指令中获取 ng-model 值

转载 作者:行者123 更新时间:2023-12-03 14:40:45 25 4
gpt4 key购买 nike

我已经在 SO 上搜索并尝试了我找到的答案,但我似乎无法从自定义指令的 ngModel 中获取模型值。

enter image description here

这是指令

/*
*usage: <markdown ng:model="someModel.content"></markdown>
*/
breathingRoom.directive('markdown', function () {
var nextId = 0;
return {
require: 'ngModel',
replace: true,
restrict: 'E',
template: '<div class="pagedown-bootstrap-editor"></div>',
link:function (scope, element, attrs, ngModel) {

var editorUniqueId = nextId++;
element.html($('<div>' +
'<div class="wmd-panel">' +
'<div id="wmd-button-bar-' + editorUniqueId + '"></div>' +
'<textarea class="wmd-input" id="wmd-input-' + editorUniqueId + '">{{modelValue()}}' +
'</textarea>' +
'</div>' +
'<div id="wmd-preview-' + editorUniqueId + '" class="wmd-panel wmd-preview"></div>' +
'</div>'));

var converter = new Markdown.Converter();

var help = function () {
// 2DO: add nice modal dialog
alert("Do you need help?");
};

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

editor.run();


// local -> parent scope change (model)
jQuery("#wmd-input-" + editorUniqueId).on('change', function () {
var rawContent = $(this).val();
ngModel.$setViewValue(rawContent);
scope.$apply();
});

// parent scope -> local change
scope.modelValue = function () {
console.log('modelvalue - ', ngModel.$viewValue);
return ngModel.$viewValue;
};
}
};
});

这是HTML
<markdown ng-class="{error: (moduleForm.Description.$dirty && moduleForm.Description.$invalid) || (moduleForm.Description.$invalid && submitted)}"
id="Description"
name="Description"
placeholder="Description"
ng-model="module.description"
required></markdown>

这里的问题是输出很简单
{{modelValue()}}

我也尝试创建一个私有(private)方法
function getModelValue() {
console.log(ngModel.$viewValue);
return ngModel.$viewValue;
}

然后将一个模板行更改为
'<textarea class="wmd-input" id="wmd-input-' + editorUniqueId + '">' + getModelValue() +

但是输出是
NaN

我哪里错了?

如果重要的话,这是我的脚本的顺序(不包括供应商脚本)
<script src="app.js"></script>
<script src="directives/backButtonDirective.js"></script>
<script src="directives/bootstrapSwitchDirective.js"></script>
<script src="directives/markdownDirective.js"></script>
<script src="directives/trackActiveDirective.js"></script>
<script src="services/alertService.js"></script>
<script src="services/roleService.js"></script>
<script src="services/moduleService.js"></script>
<script src="services/changePasswordService.js"></script>
<script src="services/userService.js"></script>
<script src="controllers/usersController.js"></script>
<script src="controllers/userController.js"></script>
<script src="controllers/moduleController.js"></script>
<script src="controllers/modulesController.js"></script>

最佳答案

您插入的 HTML 没有被编译。最简单的方法是将其移动到您的模板中,或者使用 ng-transclude 进行调查。这是将其移动到模板中的示例。

plunker

breathingRoom.directive('markdown', function () {
var nextId = 0;
return {
require: 'ngModel',
replace: true,
restrict: 'E',
template: '<div class="pagedown-bootstrap-editor"><div class="wmd-panel">' +
'<div id="wmd-button-bar-{{editorUniqueId}}"></div>' +
'<textarea class="wmd-input" id="wmd-input-{{editorUniqueId}}">{{modelValue()}}' +
'</textarea>' +
'</div>' +
'<div id="wmd-preview-{{editorUniqueId}}" class="wmd-panel wmd-preview"></div>' +
'</div></div>',
link:function (scope, element, attrs, ngModel) {

scope.editorUniqueId = nextId++;

// parent scope -> local change
scope.modelValue = function () {
console.log('modelvalue - ' + ngModel.$viewValue);
return ngModel.$viewValue;
};
}
};
});

关于angularjs - 如何在自定义指令中获取 ng-model 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com