如何在自定义指令中获取 ng-model 值 [英] How to get ng-model value inside custom directive
问题描述
我已经在这里搜索了 SO 并尝试了我找到的答案,但我似乎无法从我的自定义指令的 ngModel 中获取模型值.
这是指令
/**用法:<markdown ng:model="someModel.content"></markdown>*/呼吸室.指令('降价',函数(){var nextId = 0;返回 {要求:'ngModel',替换:真的,限制:'E',模板:'<div class="pagedown-bootstrap-editor"></div>',链接:功能(范围,元素,属性,ngModel){var editorUniqueId = nextId++;element.html($('' +'<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 转换器 = new Markdown.Converter();var 帮助 = 函数 () {//2DO: 添加漂亮的模态对话框alert("需要帮助吗?");};var editor = new Markdown.Editor(converter, "-" + editorUniqueId, {处理程序:帮助});editor.run();//本地 ->父范围更改(模型)jQuery("#wmd-input-" + editorUniqueId).on('change', function () {var rawContent = $(this).val();ngModel.$setViewValue(rawContent);范围.$应用();});//父作用域 ->局部变化scope.modelValue = 函数 () {console.log('modelvalue - ', ngModel.$viewValue);返回 ngModel.$viewValue;};}};});这里是 HTML
<markdown ng-class="{error: (moduleForm.Description.$dirty && moduleForm.Description.$invalid) || (moduleForm.Description.$invalid && 提交)}"id="说明"名称=说明"占位符=说明"ng-model="module.description"需要</markdown>
这里的问题是输出很简单
{{modelValue()}}
<小时>
我也尝试创建一个私有方法
function getModelValue() {console.log(ngModel.$viewValue);返回 ngModel.$viewValue;}
然后将一个模板行改为
'
但是输出是
NaN
我哪里出错了?
<小时>如果重要,这是我的脚本的顺序(不包括供应商脚本)
<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 进行调查.这是将其移入模板的示例.
breathingRoom.directive('markdown', function () {var nextId = 0;返回 {要求:'ngModel',替换:真的,限制:'E',模板:'<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>',链接:功能(范围,元素,属性,ngModel){scope.editorUniqueId = nextId++;//父作用域 ->局部变化scope.modelValue = 函数 () {console.log('modelvalue - ' + ngModel.$viewValue);返回 ngModel.$viewValue;};}};});
I've searched here on SO and tried the answers I found, but I can't seem to get the model value out of the ngModel of my custom directive.
Here's the directive
/*
*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;
};
}
};
});
And here's the 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>
The problem here is that the output is simply
{{modelValue()}}
I also tried creating a private method
function getModelValue() {
console.log(ngModel.$viewValue);
return ngModel.$viewValue;
}
and then change the one template line to
'<textarea class="wmd-input" id="wmd-input-' + editorUniqueId + '">' + getModelValue() +
but then the output is
NaN
Where am I going wrong?
if it matters, here's the order of my scripts (not including vendor scripts)
<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>
解决方案 The HTML your inserting isn't getting compiled. It's easiest just to move it into your template, or investigate using ng-transclude. Here's an example of moving it into your template.
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;
};
}
};
});
这篇关于如何在自定义指令中获取 ng-model 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文