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.

Question: Binding to undefined object produces error, but binding to empty object cast to interface is OK

See original GitHub issue

When I go to the hero details component (see below), I get a binding error briefly in the console because the Hero.name binding fails as the Hero is not yet back from the promise from the http service (however briefly). Makes sense. I prefer not to ngIf things either. So I did this to make the Hero initialize to an object cast to the Hero interface. Should we have to do that?

  hero: Hero = <Hero>{};

and the original code …

import {Component, View} from 'angular2/angular2';
import {RouteParams} from 'angular2/router';
import {Hero} from './hero';
import {HeroService} from './hero-service';
import {HERO_DIRECTIVES} from './hero-directives';

@Component({selector: 'my-hero-detail'})
@View({
  templateUrl: 'app/hero-detail-component.html',
  directives: [HERO_DIRECTIVES]
})
export class HeroDetailComponent {
  hero: Hero; // this is what I changed to cast it

  constructor(private _heroService: HeroService, private _routeParams: RouteParams) {
    let id = +_routeParams.get('id');
    this._heroService.getHero(id).then(hero => this.hero = hero);
  }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
pkozlowski-opensourcecommented, Sep 13, 2015

I get a binding error briefly in the console because the Hero.name binding fails as the Hero is not yet back from the promise

@johnpapa are you talking about a binding in a template? Sth like {{hero.name}}? If so you can use the Elvis operator - sth like {{hero?.name}}.

Should we have to do that?

Hope that the above answers the question so going to close this one for now. Happy to re-open if more discussion is needed and / or your use-case is different.

4reactions
pkozlowski-opensourcecommented, Sep 13, 2015

I am just curious why this fixes it too hero: Hero = <Hero>{};

Well, assuming that in your template you’ve got sth like {{hero.name}} in a template the hero: Hero = <Hero>{}; line “works” since it initialises the hero field to an empty object instead of null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting undefined error during two-way form binding when ...
This gives error because interface is expecting name, email and address - so cant be declared as empty. If I remove the interface...
Read more >
Classes - pybind11 documentation
This section presents advanced binding code for classes and it is assumed that you are already familiar with the basics from Object-oriented code....
Read more >
Error Messages - SQLAlchemy 1.4 Documentation
A bind was located via legacy bound metadata, but since future=True is set on this Session, this bind is ignored. This Compiled object...
Read more >
Understanding and using interfaces in TypeScript
A class or function can implement an interface to define the implementation of the properties as defined in that interface. Over 200k developers ......
Read more >
Jdbi 3 Developer Guide
Jdbi instances are thread-safe and do not own any database resources. ... Jdbi makes use of mappers to convert result data into Java...
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