question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: one-time boolean bindings

See original GitHub issue

πŸš€ feature request

Relevant Package

This feature request is for @angular/core and @angular/compiler? I am not very familiar with the infrastructure.

Description

First of all, I am not referring to the AngularJS one-time bindings (:: prefix) in any capacity. This request is a logical extension of the one-time string bindings which already exist in the new Angular.

<!-- Regular binding -->
<mat-icon [fontIcon]="someIconVariable"></mat-icon>

<!-- One-time binding to literal string -->
<mat-icon fontIcon="cloud-upload"></mat-icon>

I have found the above feature of Angular to be exceptionally nice (along with many others!) and could not care less that we no longer have explicit one-time bindings to variables as they made it easy to introduce bugs and the performance gains pale in comparison to those netted by AOT compilation anyways.

However, a common pattern in AngularJS material, as well as many of my own components, is to support empty attributes for boolean properties, which would be the same as binding to a literal true. I believe empty attributes are not technically valid XML, but it is a common practice in HTML where the presence of an attribute makes it truthy and the absence of the attribute makes it falsy, such as putting required on an <input>.

With my proposed change, the following two examples would be equivalent.

<!-- This works -->
<user-card [userID]="selectedUserID" [showAdminControls]="true"></user-card>

<!-- This would be nice :^) -->
<user-card [userID]="selectedUserID" showAdminControls></user-card>

With this feature, you can either bind a boolean to the input as usual or just include the attribute without a binding, causing Angular to set the property to true and then leave it alone, just as the existing one-time string literal bindings work.

Describe the solution you’d like

Sorry, but I am not familiar with the Angular source code, so I have no idea how this would be implemented. The best I can do for now is to write a pretty issue and hope it garners some attention.

Describe alternatives you’ve considered

To implement this on my components, I basically just set the initial value of the property to whether the attribute is present and empty.

// ...
export class UserCardComponent {
    // ...
    @Input() showAdminControls = hasEmptyAttr(this.host.nativeElement, "showAdminControls");
    // ...
}

This approach is tedious and requires the programmer to keep the property/attribute names in sync. AFAIK the template compiler always reports unrecognized properties it finds in the HTML but a typo in the string in the hasEmptyAttr(...) call would result in a subtle bug.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
samclauscommented, Jan 11, 2020

@thefliik Yes, that is what I am suggesting. Setting it to an empty string is rather useless.

1reaction
angular-robot[bot]commented, Jun 26, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn’t collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular’s scope, we’d encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJs one time binding and order of values with boolean ...
In Angular, one time binding will constantly evaluate the expression until it has a non- undefined value. So in your example, because nameΒ ......
Read more >
How to use Boolean to control visibility of item dynamically
How can I dynamically use a Boolean that I created to control the visibility of an item via a decision making statement?
Read more >
A Boolean Map Theory of Visual Attention
A theory is presented that attempts to answer two questions. What visual contents can an observer consciously access at one moment? Answer: only...
Read more >
The "checked" binding - Knockout.js
If you give a value that isn't actually boolean, it will be interpreted loosely. This means that nonzero numbers and non- null objects...
Read more >
Boolean Datatype - Ask TOM
With one time coding, up to 2^8/3 = 85 boolean columns could fit in 1 byte (though likely 2 bits per column for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found