2014-09-23 111 views
2

我创建了一个使用隔离范围的指令。我从视图中传递一个变量。将隔离范围变量从指令传递到其控制器

DIRECTIVE

function abcDirective() { 
    return { 
     restrict: 'AEC', 
     templateUrl: 'abc.html', 
     controller: 'ABController as abCtrl', 
     scope: { 
      dataSent: '=' 
     } 
    } 
} 

THE VIEW

<div abc-directive data-sent="{some: Object}"></div> 

现在,当我打通Batarang,我看到ABCtrl的一个对象的所有范围元素。和一个对象有{some: object}。我希望这个{some: object}成为ABCtrl的一部分。我怎样才能做到这一点 ?

谢谢。

+0

我不知道我完全理解。你可以在js小提琴中做这个,或者包含你的控制器代码plz – Cathal 2014-09-23 16:11:43

回答

3

有一个new feature in 1.3,允许您通过指令定义上的bindToController属性指定此值。

{ 
    scope: { 
    dataSent:'=' 
    }, 
    bindToController:true 
} 

在那之前,你将不得不做手工,无论是在逻辑函数,或指令控制器内:

{ 
    //Using the link function 
    link:function(scope, elem, attrs, ctrl){ 
     ctrl.dataSent = scope.dataSent; 

     scope.$watch('dataSent', function(){ 
     ctrl.dataSent = scope.dataSent; 
     }); 
    } 
} 

//Using the controller 
var ABCController = function($scope){ 
    this.dataSent = $scope.dataSent; 

    $scope.$watch('dataSent', function(){ 
     this.dataSent = $scope.dataSent; 
    }.bind(this)); 
} 
相关问题