Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Boolean Value Into Directive

I am trying to toggle the visibility of an element rendered by a directive using <div ngHide="readOnly">. The value or readOnly is passed in via a directive.

angular.module('CrossReference')     .directive('singleViewCard', [function() {         return {             restrict:'AE',             templateUrl: '/CrossReference-portlet/js/templates/SingleViewCard.html',             replace:true,             scope: {                 readOnly:'@'              },             link: {                 pre:function(scope, tElement, tAttrs) {},                  post:function(scope, tElement, tAttrs) {};                 }             }         };     }]); 

This seems to work in the following cases:

<!-- element rendered in single-view-card is hidden --> <div single-view-card read-only="{{true}}"/>  <!-- element rendered in single-view-card is shown --> <div single-view-card read-only="{{false}}"/> 

However, if I invert the boolean expression <div ngHide="!readOnly"> The following usage of the directive does not hide the dive as expected:

<!-- element is rendered when it should be hidden --> <div single-view-card read-only="{{false}}"/> 

What am I doing wrong?

like image 386
timmy Avatar asked Sep 12 '14 01:09

timmy


People also ask

How do you pass a boolean value to a string?

toString(boolean b): This method works same as String. valueOf() method. It belongs to the Boolean class and converts the specified boolean to String. If the passes boolean value is true then the returned string would be having “true” value, similarly for false the returned string would be having “false” value.

Which angular structural directive takes a boolean expression and makes an entire chunk of a DOM appear or disappear?

NgIf is the simplest structural directive and the easiest to understand. It takes a boolean expression and makes an entire chunk of the DOM appear or disappear. The ngIf directive doesn't hide elements with CSS.

How do you define a boolean variable in TypeScript?

Boolean values are supported by both JavaScript and TypeScript and stored as true/false values. TypeScript Boolean: let isPresent:boolean = true; Note that, the boolean Boolean is different from the lower case boolean type.


1 Answers

what you are doing wrong is

readOnly:'@' 

this means readOnly will be a string, to make it a js variable try

readOnly:'=' 

then

<div single-view-card read-only="{{false}}"/> 

should be

<div single-view-card read-only="true"/> 

you need to show more code, this can be part of the error but I think there is more

hope it helps

like image 109
bto.rdz Avatar answered Sep 22 '22 22:09

bto.rdz