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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With