问题描述
我想访问链接函数中的 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.
但是没有调用链接函数.Plunker 链接
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
};
它的工作.
这篇关于angularjs 指令链接函数未从 compilefunction 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!