I wrote a pipe to handle this, but that seems like over kill. Testing for
a key in an object is basic, but a critical operation.
Does NG2 really not allow do this, or am I just missing something?
@Pipe({name: 'matchkey'})
export class MatchkeyPipe implements PipeTransform {
transform(obj:any, keys:any[]):any {
try {
if (obj && keys) {
return keys.find(key => obj[key])
}
} catch (e) {
console.log('...objectkey error ', e);
}
}
}
On Monday, July 18, 2016 at 9:43:24 AM UTC-7, p. stephen w wrote:
>
> NgInit is currently calling my service to return the Json.
> Is it true that the template doesn't get parsed until controller/component
> has ran?
>
> How can I use an elvis (LOL, love the name)? I'm assuming that's a
> is-nullable operator?
>
>
>
>
>
> On Sunday, July 17, 2016 at 7:53:53 PM UTC-7, Sander Elias wrote:
>>
>> Hi P. Stephen,
>>
>> Not entirely. The problem is that probably the myObj is undefined when
>> the component initializes. Have you tried setting it to an empty object
>> directly on your controller's constructor?
>> Usually when you need something like this, you can use the elvis(?)
>> operator to protect against undefined parts. This is not going to work for
>> array notation.
>> This does work for me:
>>
>> import { Component } from '@angular/core';
>>
>>
>> @Component({
>> selector: 'my-app',
>> template: `<h1>My First Angular 2 App</h1>
>> {{myObj['test']}}
>> <pre>{{myObj|json}}</pre>
>> `
>> })
>> export class AppComponent {
>> myObj = {}
>> ngOnInit() {
>> this.myObj= {test : "hello"}
>> }
>> }
>>
>>
>> Hope this helps you a bit,
>> Regards
>> Sander
>>
>>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.