角JS指令定义 - 要求ngModelController以及自定义控制器 [英] Angular JS Directive Definition - Require ngModelController as well as a custom controller

查看:214
本文介绍了角JS指令定义 - 要求ngModelController以及自定义控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写我的角度指令有他们的大部分在其控制器的code的功能,只被用于两种不同的指令控制器绑在一起的纽带作用。

I'm trying to write my Angular directives to have most of their functionality in the code of their controllers, with the link function just being used to bind two different directive controllers together.

我遇到一个问题,当谈到这两个require'ing控制器,并具有定义为井控制器。我怎么得到的链接功能,我指定控制器的参考?

I'm running into a problem when it comes to both 'require'ing a controller, and having the 'controller' defined as well. How am I supposed to get a reference in the link function to the controller I've specified?

样code(当然没有周围所有的东西module.directive)

Sample code (without of course all the surrounding module.directive stuff)

return {
    require:'ngModel',
    controller: 'CustomDirectiveCtrl',
    link: function(scope, element, attrs, controllers)
    {
        console.log(controllers);
    }
};

控制器包含ngModelController,但不给我一个方式来访问CustomDirectiveCtrl。

'controllers' contains ngModelController, but doesn't give me a way to access CustomDirectiveCtrl.

我如何能够访问ngModelController和CustomDirectiveCtrl,从链接功能?

How can I access both ngModelController, and CustomDirectiveCtrl, from the link function?

推荐答案

有时候,你永远,直到你尝试解释给别人发现一个问题。

Sometimes you never find a problem until you try explaining it to someone else.

看源角JS的ngModel后,我找到了解决办法。以下是如何解决我上面code,以防万一别人运行到认识了同样的问题:

After looking at the source for Angular JS's ngModel, I found the solution. Here's how to fix my above code, just in case anyone else is running into the same problem of understanding:

https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L2347

module.directive('customDirective', function(){
    return {
          require:['customDirective', 'ngModel'],
          controller: 'CustomDirectiveCtrl',
          link: function(scope, element, attrs, controllers){
               // CustomDirectiveCtrl is controllers[0],
               // NgModelController is controllers[1]
               console.log(controllers);
          }
    }
});

我认为是让你自己的控制器,让VS不同型号的控制器不同的语法有必要的。它比我想象的更简单。

I thought there needed to be a different syntax for getting your own controller, vs getting a different model's controller. It's simpler than I thought.

这篇关于角JS指令定义 - 要求ngModelController以及自定义控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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