angularjs 指令链接函数未从 compilefunction 调用 [英] angularjs directive link function not called from compilefunction

查看:30
本文介绍了angularjs 指令链接函数未从 compilefunction 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问链接函数中的 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 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆