2015-11-02 76 views
0

當父控制器中的某些元素有事件時,我可以如何顯示/隱藏指令?父控制器的角度切換指令

app.directive('rest', function(){ 
 
    return { 
 
    restrict: 'E', 
 
    scope: false, 
 
    replace: true, 
 

 
    link: function(scope, elem, attr) { 
 
    
 
    }, 
 
    templateUrl: '<div ng-show="showDirective"></div>', 
 
    } 
 
});
<div ng-controller="AppCtrl"> 
 
    
 
    <rest></rest> 
 
    
 
<div ng-click="showDirective = false"><div> <!-- hide directive --> 
 
<div ng-click="showDirective = true"><div> <!-- show directive --> 
 
</div>

+0

我沒有看到任何東西代碼錯誤。請給我一個plnkr與請玩。確保父控制器上的值正在改變。 – VSO

回答

0

在這裏,你的錯誤是 「templateUrl」 而不是 「模板」。當您提供HTML在線,用戶的「模板」,當你在一個單獨的HTML文件提供HTML中,使用「templateUrl」,並提供一個URL到HTML模板:

http://plnkr.co/edit/xzqOvcjKYfWxazWfHWyS?p=preview

.directive('rest', function(){ 
    return { 
     restrict: 'E', 
     scope: false, 
     replace: true, 


    link: function(scope, elem, attr) { 

    }, 
    template: '<div ng-if = "showDirective" >Test</div>', 
}