Question: Binding to undefined object produces error, but binding to empty object cast to interface is OK
See original GitHub issueWhen 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:
- Created 8 years ago
- Comments:19 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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}}
.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.
Well, assuming that in your template you’ve got sth like
{{hero.name}}
in a template thehero: Hero = <Hero>{};
line “works” since it initialises thehero
field to an empty object instead of null.