2

我在学习如何正确使用自定义指令的bindToController功能,并想知道如何从指令控制器访问您在bindToController对象中声明的属性。Angular 1.5:访问控制器内的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这里:http://jsfiddle.net/jrtc1bLo/2/

于是我想到了性能结合到控制器,但似乎他们宁愿绑定到范围控制器别名。

什么是从控制器访问它们的好方法?

感谢

+0

你能解释一下你在做什么吗?我想你可能会在不知不觉中滥用bindToController。有可能你可以一起避免它,只是使用父范围。 – Lansana

+0

'$ scope。$ watch('ctrl.name',...'不会绑定到任何有用的东西,因为您将'name'绑定到控制器而不是范围,您应该执行'$ scope。$ watch(函数(){return this.name;},...)'? – Mike

+0

@Mike你的答案的第二部分几乎是正确的,但'this'没有引用我的控制器,所以'return ctrl。name'作品(cf @georgeawg答案) – ValLeNain

回答

1

如果您更正你的第一个观察者,你会看到你的控制器绑定正确。

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; 
    }) 
} 

的第一个参数$watch要么被评估每消化周期或者被评估的每个消化循环中的字符串形式的角表达式的函数。

随着更正,两位观察者将看到变化。

0

看看这个例子:

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; 
} 

正如你所看到的,我有一个form-group-item指令。我用它在我的HTML像这样:

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

下面我就三样东西从该指令,使用了范围,我基本上是复制他们(它们绑定到控制器)的指令本身。这三件事是:模型,错误提交

错误是一堆可能的错误(数组),模型仅仅是一个值,而提交是一个提交函数。

现在这三件事都在FormGroupItemController的范围内,我可以在form-group-item.html中将它们用作$scopecontrollerAs,无论我指定哪一个。在这种情况下,我使用了controllerAs语法。

所以指令之前有什么绑定到它的控制器,它只是一个空的构造是这样的:

function FormGroupItemController() {} 

绑定后,它看起来就像这样:

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

如果你想对你的FormGroupItemController中的'model','errors'或'submit'进行一些计算?像检查模型是好的,还是格式化错误? – ValLeNain

+0

你可以在FormGrouItemController中做任何你想做的事情。这只是显示如何将一个控制器的功能绑定到指令控制器的基本类型。一旦你将他们绑定,你可以做任何你想要的。 – Lansana

+0

编辑后,可以显示控制器在绑定时的样子。然后,您可以像访问其他任何控制器一样访问/操作它们。 – Lansana