Angular Directive - 需要ngModel和表单^

时间:2017-07-19 23:57:45

标签: angularjs angularjs-directive

在angular指令中你可以要求表格^ AND ngModel吗?是否可以访问链接功能中的表单和控制器?

angular.module('myApp')
.directive('passwordValidation', passwordValidation)
function passwordValidation(){
  return {
    replace: true,
    require: 'ngModel', // also 'form^' ?
    scope: {
      userName: '@'
    },
    link: function(scope, elem, attr, ctrl, form) {
    ...

1 个答案:

答案 0 :(得分:2)

是的,可以通过在require属性中提供数组来实现。使用它,您将获得链接函数中第4个参数的控制器数组。

angular.module('myApp')
.directive('passwordValidation', passwordValidation)
function passwordValidation(){
  return {
    replace: true,
    require: ['ngModel','^form'], // also 'form^' ?
    scope: {
      userName: '@'
    },
    link: function(scope, elem, attr, controllers) {
      var ngModelCtrl = controllers[0],formCtrl = controllers[1]; 
    }
  }}