我想在链接函数中访问ngModelController。我正在使用$compile根据用户选项动态生成 html。根据文档,我需要从编译函数返回链接函数。
但是链接函数没有被调用。普伦克链接
我试图限制用户在类型=数字时输入字母。
编辑
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..从编译函数返回
你只需要替换"require"而不是"required"
即
return {
scope: { options: '=', ngModel: '=' },
require: ['ngModel', '^form'],
restrict: 'E',
compile: compileFunction
};
这是工作。
编译函数已经返回一个函数(即链接函数):
var compileFunction = function (element) {
return function (scope) { // linking function
};
};
您可以手动调用函数:
var compileFunction = function (element) {
return function (scope) { // linking function
// ...
linkFunction.apply(this, arguments);
};
};
按照
你的定义
var compileFunction = function (element) {
return function (scope) { //<- This is your link function
....
return (linkFunction); // not this
};
};
另外,我认为该范围无法在编译函数中使用,因为您使用的类似
var template = helper.getFieldHtml(fieldHtml, scope.options);
element.html(template);
$compile(element.contents())(scope);
如果你想在编译函数中调用这三行。