Angularjs 如何将NgModelController传递给指令控制器?

Angularjs 如何将NgModelController传递给指令控制器?,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我可以将NgModelController传递给指令控制器吗?这是必需的,所以我可以在控制器中为模型赋值 此示例不起作用: angular .module('directives.selectBox', []) .directive('selectBox', selectBox); function selectBox() { return { restrict : 'E', require

我可以将NgModelController传递给指令控制器吗?这是必需的,所以我可以在控制器中为模型赋值

此示例不起作用:

   angular
     .module('directives.selectBox', [])
     .directive('selectBox', selectBox);  

    function selectBox() {
        return {
          restrict   : 'E',
          require    : 'ngModel',
          scope      : {
             list     : '=',
          },
          replace     : true,
          templateUrl : 'common/directives/selectBox/selectBox.html',
          controller :  SelectBoxController,
        };
    }  
    function SelectBoxController(ngModel) {
       ngModel.$setViewValue(10); // ???
    }

首先,我注意到您的控制器没有在模块中声明,因此它没有被调用。(可能还需要对其进行重命名,以避免混淆。)

angular
     .module('directives.selectBox', [])
     .controller('SelectBoxController',SelectBoxController)
     .directive('selectBox', selectBox);
该指令采用ng模型。然后您可以在该指令的链接函数中使用$scope.ngModel访问它

function selectBox() {
        return {
          restrict   : 'E',
          require    : 'ngModel',
          scope      : {
             list     : '=',
          },
          replace     : true,
          templateUrl : 'common/directives/selectBox/selectBox.html',
          controller :  SelectBoxController,
          link: function($scope, $element, $attr, ngModel) {

                $scope.ngModel.$setViewValue(10);
           }
        };
这里甚至不需要控制器。 请注意,您不一定需要ngModel,您已经在这里将“list”作为双向绑定参数传递,因此如果您想将另一个参数作为参数传递,只需像隔离作用域中的“list”参数一样传递即可。 您也不应该直接分配给您传递的ng模型或参数,而是分配给对象的“子”元素,否则您将看到奇怪的行为,因为您的本地对象将覆盖父范围的原始对象

因此,您可以将“model.list”作为“list”参数传递,然后在本地范围中修改“list”,这将修改父范围中的“model.list”。
如果您传递'list'并在本地作用域中修改它,它将覆盖父作用域中的列表,并且可能无法获得您期望的双向绑定。

非常简单,实际上,您需要做的是通过将
$element
注入控制器,然后调用
.controller()来访问元素
它的功能

angular
   .module('directives.selectBox', [])
   .directive('selectBox', selectBox);  

function selectBox() {
    return {
      restrict   : 'E',
      require    : 'ngModel',
      scope      : {
         list     : '=',
      },
      replace     : true,
      templateUrl : 'common/directives/selectBox/selectBox.html',
      controller :  SelectBoxController,
    };
}  
function SelectBoxController($element) {
   var ngModel = $element.controller('ngModel');
   ngModel.$setViewValue(10); // ???
}
角度1.5更新 请注意,在AngularJS 1.5中,除了现有的
directive()
函数之外,还添加了新的
component()
函数。此函数将configuratoin对象作为第二个参数,允许您直接指定所需的控制器,然后将其绑定到组件的控制器

下面同样的例子,但作为组件

angular
   .module('directives.selectBox', [])
   .component('selectBox', 
        {
            controller: SelectBoxController,
            controllerAs: 'vm',
            templateUrl : 'common/directives/selectBox/selectBox.html',
            bindings: {
                list: '=' // though '<' would be better
            }, 
            require: {
                ngModel: '='
            },
            // replace: true ==> No longer possible with component
        }
    );  

function SelectBoxController($element) {

    $onInit() {
        // This function is called automatically whenever the controller is ready
        this.ngModel.$setViewValue(10); // ???
    }
}
angular
.module('directives.selectBox',[])
.component('selectBox',
{
控制器:选择BoxController,
controllerAs:'vm',
templateUrl:'common/directions/selectBox/selectBox.html',
绑定:{

列表:'='//尽管'如果您只想影响模型值,$parse方法最适合您,因为它提供了与外部作用域通信并编写代码的通用方法,而不依赖于新作用域的创建

angular.directive('selectBox', function($parse){
    return {
       // your directive stuff
       link: function(scope, iElem, iAttrs){
           var angularVarSetter = $parse(iAttrs.ngModel).assign;
           // in case of no own scope.
           angularVarSetter(scope, 100500);
           // in case of new scope
           angularVarSetter(scope.$parent, 100500);
       }
    };
});

您可以将其注入链接函数,而不是控制器。使用指令语法“controller as”。这种技术在这种情况下会变得很糟糕?@UfukHacıoğullarıngModel不能被注入,它只能作为链接函数中的参数使用,即使你不使用它,当你需要ngModel时,它仍然可以作为链接函数中的参数使用。它不同于依赖注入。有人能为它创建一个小提琴吗?我有点担心与模型partnotworking.console.log($scope.ngModel)->undefined混淆“您的控制器未在模块中声明,因此未调用”这是不正确的。如果您查看
$compile
的文档,您会发现这可能是指令的控制器的构造函数。这听起来是一个非常简单的解决方案。我只是在想,在谈论真正的控制器间通信时,使用事件处理控制器间通信是否不是一个更好的主意troller通信(在单独的控制器之间)我同意应该使用事件或服务。但是,在这种情况下,我觉得控制器只是指令的一部分,因此在指令创建函数和它的控制器之间有一个紧密的联系不是问题。请注意,
angular.component()当前不提供require绑定
s对于1.5.x以下的角度版本,使用角度组件多边形填充创建