来自父控制器的Angular toggle指令

时间:2015-11-02 16:03:09

标签: angularjs angularjs-directive

当父控制器中的某些元素有事件时,我如何显示/隐藏指令?



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

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

1 个答案:

答案 0 :(得分:0)

在这里,你的错误是&#34; templateUrl&#34;而不是&#34;模板&#34;。当您提供html内联,用户&#34;模板&#34;时,如果您在单独的html文件中提供html,请使用&#34; templateUrl&#34;并为模板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>',
}