问题描述
我想在链接功能中访问ngModelController.我正在使用$ compile根据用户选项动态生成html.根据文档,我需要从compileFunction返回链接函数.
I want to access the ngModelController in the linking function.I am using $compile to generate the html dynamically based on the user options.as per the docs I need to return the linking function from compileFunction.
,但未调用链接功能.柱塞链接
but the linking function is not getting called.Plunker Link
我试图限制用户在输入type = number时不能输入字母.
I am trying to restrict user from entering alphabets when type=number.
编辑
var compileFunction = function (element) {
return function (scope, telem, tattr, ngModelCtrl) {
console.log(ngModel);
var template = helper.getFieldHtml(fieldHtml, scope.options);
element.html(template);
$compile(element.contents())(scope);
return linkFunction.apply(scope, telem, tattr, ngModel);
};
};
return {
scope: { options: '=', ngModel: '=' },
required: ['ngModel', '^form'],
restrict: 'E',
compile: compileFunction
};
如何访问链接函数中的ngModelCtrl ..从编译函数返回
how to I access ngModelCtrl in the link function.. returned from compile function
推荐答案
您只需要替换"require"而不是"required"
You just need to replace "require" instead of "required"
即
return {
scope: { options: '=', ngModel: '=' },
require: ['ngModel', '^form'],
restrict: 'E',
compile: compileFunction
};
正在工作.
这篇关于未从compilefunction调用angularjs指令链接函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!