Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS custom directive ng-show/ng-hide

Warning: Angular newbie ahead.

I'm trying to create a custom widget that will show by default a "Reply" link, and when clicked, it should be hidden and a textarea should be shown. Here is what I have so far, but it doesn't work::

  .directive('replybox', function ($rootScope) {
    var linkFn = function (scope, element, attrs) {
        var label = angular.element(element.children()[0]);
        scope.showInput = false;

        label.bind("click", textbox);

        function textbox() {
            scope.showInput = true;
        }
    };
    return {
        link:linkFn,
        restrict:'E',
        scope:{
            id:'@',
            label:'@',
            showInput:'='
        },
        template:'<a ng-hide="showInput">label</a><textarea ng-show="showInput">    </textarea>',
        transclude:true
    };
})

Any guideline will be appreciated. Thanks!

like image 352
Matt Avatar asked Jul 31 '12 13:07

Matt


People also ask

Can we use ng-show and Ng-hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.

What is the difference between ng-show ng-hide and Ng if directives?

Basic Difference between ng-if, ng-show and ng-hideng-if can only render data whenever the condition is true. It doesn't have any rendered data until the condition is true. ng-show can show and hide the rendered data, that is, it always kept the rendered data and show or hide on the basis of that directives.

How do you show hidden data when the particular condition is true in AngularJS?

How to show/hide data when the particular condition is true in AngularJS ? In AngularJS, in order to hide or show data or content, we can use the *ngIf structural directive. By using this, we can evaluate conditions and then *ngIf based on the condition displays the content.

Is ng-show a directive?

AngularJS ng-show DirectiveThe ng-show directive shows the specified HTML element if the expression evaluates to true, otherwise the HTML element is hidden.


1 Answers

Matias, here is a working jsFiddle: http://jsfiddle.net/pkozlowski_opensource/Z6RzD/

There were number of things going on really, but I think that the most important one was the fact that you need to use Scope.$apply to have angular notice scope changes from 'outside of anular's world'. By default angular doesn't trigger templates re-evaluation on each and every DOM event so you need to wrap it into apply:

scope.$apply('showInput = true');

More info here: http://docs.angularjs.org/api/ng.$rootScope.Scope

There are also other items worth noticing in your example:

  • I guess you would like to be able to pass 'label' as an attribute to your directive and then use it in your template - if so you need to use {{label}} expression
  • I wasn't quite sure what would be the use for the 'id' attribute so omitted it from my fiddle
  • Same for the 'showInput' - couldn't quite figure out what is the thing you are trying to get
like image 94
pkozlowski.opensource Avatar answered Sep 30 '22 20:09

pkozlowski.opensource