2016-07-14 68 views
1

我有一个简单的指令:隔离指令结合于母公司的功能

let directive = { 
    restrict: 'EA', 
    templateUrl: 'app/components/video-player/video-player.html', 
    scope: { 
     someFunction:'=' 
    }, 
    ... 
} 

template: 
<div class="video" ng-click="vm.someFunction(vm.someId)"></div> 

directive: 
<video-player some-function="main.ctaClick"></video-player> //controllerAs main 

export class MainController { 
    ... 
    someFunction(){ 
     // How do I get the correct this here without using $parent? 
     let context = this.scope.$parent.main; 
    } 
} 

基本上我想知道是否有使用父范围的情况下一种方式,当双向绑定这样的功能?这是正确的方法吗?

回答

1

孤立的指令是从它的父母,因此名称分离。如果父母应该向指令提供某些内容,则应将其作为属性进行传递。

可以使用继承范围and use bindToController binding instead替换隔离范围。但是,这可能表明一个设计缺陷。

这是正确的方法吗?