简体   繁体   English

Angular 1.5:访问控制器内的 bindToController 属性

[英]Angular 1.5: Access bindToController properties inside controller

I'm learning how to properly use the bindToController feature of custom directives and wondering how to access the properties you declare in the bindToController object from the directive controller.我正在学习如何正确使用自定义指令的 bindToController 功能,并想知道如何从指令控制器访问您在 bindToController 对象中声明的属性。

var myApp = angular.module('myApp',[])
  .directive('myDir', MyDir)
  .controller('MyCtrl',['$scope', MyCtrlFn]);

function MyCtrlFn($scope) {
  var ctrl = this;
  this.ctrlStr = '';
  this.ctrlAsStr = '';
  $scope.$watch(this.name, function(newValue) {
    if(newValue) ctrl.ctrlStr += ' '+newValue;
  })
  $scope.$watch('ctrl.name', function(newValue) {
    if(newValue) ctrl.ctrlAsStr += ' '+newValue;
  })
}

function MyDir() {
  return {
    template: '<div>{{ctrl.name}}</div>'+
    '<div>CtrlStr: {{ctrl.ctrlStr}}</div>'+
    '<div>CtrlAsStr: {{ctrl.ctrlAsStr}}</div>',
    scope: {},
      bindToController: {
          name: '='
      },
    restrict: 'E',
    controller: 'MyCtrl',
    controllerAs: 'ctrl'
  }
}

jsFiddle here: http://jsfiddle.net/jrtc1bLo/2/ jsFiddle在这里: http : //jsfiddle.net/jrtc1bLo/2/

So I thought the properties were bound to the controller but it seems they're rather bound to the controller alias in the scope.所以我认为属性绑定到控制器,但似乎它们绑定到范围内的控制器别名。

What's the good way to access them from the controller ?从控制器访问它们的好方法是什么?

Thanks谢谢

If you correct your first watcher, you will see that your controller is bound correctly.如果您更正第一个观察者,您将看到您的控制器已正确绑定。

function MyCtrlFn($scope) {
  var ctrl = this;
  this.ctrlStr = '';
  this.ctrlAsStr = '';
  //DO THIS
  $scope.$watch(function(){return ctrl.name}, function(newValue) {    
  // NOT THIS
  //$scope.$watch(this.name, function(newValue) {
    if(newValue) ctrl.ctrlStr += ' '+newValue;
  })
  $scope.$watch('ctrl.name', function(newValue) {
    if(newValue) ctrl.ctrlAsStr += ' '+newValue;
  })
}

The first argument to $watch is either a function that is evaluated every digest cycle or an Angular expression in string form which is evaluated each digest cycle. $watch的第一个参数要么是一个在每个摘要周期评估的函数,要么是一个字符串形式的 Angular 表达式,它在每​​个摘要周期评估。

With the correction, both watchers will see the changes.通过更正,两个观察者都会看到变化。

Take a look at this example:看看这个例子:

angular
    .module('form')
    .controller('FormGroupItemController', FormGroupItemController)
    .directive('formGroupItem', formGroupItem);

function FormGroupItemController(){}

function formGroupItem() {
    var directive = {
        restrict: 'A',
        scope: {
            model: '=',
            errors: '=',
            submit: '='
        },
        bindToController: true,
        controller: 'FormGroupItemController as vm',
        templateUrl: 'app/form/form-group-item.html'
    };

    return directive;
}

As you can see, I have a form-group-item directive.如您所见,我有一个form-group-item指令。 I use it in my HTML like so:我在我的 HTML 中使用它,如下所示:

<div form-group-item model="vm.username" errors="vm.errors" submit="vm.updateUsername"></div>

Here I take three things from the scope that the directive was used in, and I essentially duplicate them (bind them to the controller) of the directive itself.在这里,我从指令使用的范围中提取了三样东西,我基本上复制了指令本身的它们(将它们绑定到控制器)。 Those three things are: model , errors , and submit .这三件事是:模型错误提交

The errors are a bag of possible errors (array), the model is just a value to watch, and the submit is a submit function.错误是一包可能的错误(数组),模型只是一个要观察的值,提交是一个提交函数。

Now those three things are within the scope of FormGroupItemController , and I can use them in the form-group-item.html as a $scope or controllerAs , whichever I specified.现在这三件事都在FormGroupItemController的范围内,我可以在form-group-item.html用作$scopecontrollerAs ,无论我指定哪个。 In this case, I use controllerAs syntax.在这种情况下,我使用 controllerAs 语法。

So before the directive has anything binded to it's controller, it's just an empty constructor like this:所以在指令绑定到它的控制器之前,它只是一个像这样的空构造函数:

function FormGroupItemController() {}

After the bind, it will look like this:绑定后,它将如下所示:

function FormGroupItemController() {
    this.model = 'blah';
    this.errors = [...];
    this.submit = function(...) {...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM