Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NgClass and empty string as a class name #4033

Closed
wardbell opened this issue Sep 7, 2015 · 6 comments
Closed

NgClass and empty string as a class name #4033

wardbell opened this issue Sep 7, 2015 · 6 comments

Comments

@wardbell
Copy link
Contributor

wardbell commented Sep 7, 2015

Example:

// hero-detail-component.html
<input [(ng-model)]="hero.name"  class="foo" [ng-class]="addClass()"></input>

// hero-detail-component.ts
addClass() {
  return this.hero.name.length > 5 ? "selected" : "";
}

Assume hero in this detail changes as a new hero is selected from a parent list

  1. 1st hero is "Mr. Long Name"
  2. 2nd hero is "short"
    transpiler: show line numbers that have errors #1 First hero displays and the class "selected" is present
    Integrate dartanalyzer into build #2 Second hero triggers change in value. Angular fails with exception

DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided must not be empty.(…) thrown in NgClass.onCheck in the line this._applyIterableChanges(changes);

Also fails the same way when addClass is:

addClass() {
  return this.hero.name.length > 5 ? "selected" : " notSelected"; // leading space
}

But "works" if it is:

addClass() {
  return this.hero.name.length > 5 ? "selected" : "notSelected";} // no space
}

The array-result form is OK

  addClass() {
    return this.hero.name.length > 5 ? ["selected"] : [];
  }

And the object-result form is OK

  addClass() {
    return {"selected": this.hero.name.length > 5};
  }

@pkozlowski-opensource pkozlowski-opensource changed the title ngmodel NgClass and empty string as a class name Sep 13, 2015
@pkozlowski-opensource
Copy link
Member

I've renamed this issue as it has nothing to do with NgModel but the crux of the issue is what NgClass does with empty strings.

@pkozlowski-opensource
Copy link
Member

@wardbell I wonder how we should fix it. My first reaction is to trim strings representing class names and simply ignore empty (blank) strings. WDYT?

@wardbell
Copy link
Contributor Author

Trim and ignore extra white space for sure.

Empty string should be ignored. It means "no additional class names".

I would not mind if this string variant of the NgClass API was removed. The object notion is the most clear and perhaps easier to construct.

@pkozlowski-opensource
Copy link
Member

@wardbell right, PR #4173 trims class names. Regarding removal of the string-based form - I don't use it often but it comes handy when you need to calculate class name dynamically (that it, you don't know it up-front). Not super-common, but happens.

BTW, in your particular use case the easiest approach would be to go without any directive at all and use the special-cased syntax from the compiler:

<input [(ng-model)]="hero.name" class="foo" [class. selected]="hero.name.length > 5">

@wardbell
Copy link
Contributor Author

Yes I have used that and it is worth teaching. Perfect for setting a single class value. Not so great when you have multiple class names to set. So many ways to skin this cat. Are there ever enough? ;-)

p.s.: I have no problem calculating a class name dynamically and adding it to the definition object with object indexing: classes[theName] = theValue. Not quite as cool as composing the string with back-tick interpolation but serviceable.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants