HaithemMosbahi / ngx-avatar

Universal avatar component for angular 2+ applications makes it possible to fetch / generate avatar from different sources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to call ngx-avatar's getRandomColor

bhalperin opened this issue · comments

In my Angular 7 app I successfully integrated ngx-avatar 3.2.0. However, I can't find how to call AvatarService.getRandomColor(). In my component file I have:

import { AvatarService } from 'ngx-avatar/lib/avatar.service';
...
constructor(private avatarService: AvatarService) { }
...
const color = this.avatarService.getRandomColor(name);

But my compilation fails with the following error:

ERROR in some.component.ts
Module not found: Error: Can't resolve 'ngx-avatar/lib/avatar.service' in 'd:\projects\...'

I changed it as follows to make it work. But I don't think it was the author's intention:

import * as Avatar from 'ngx-avatar';
...
constructor(private avatarService: Avatar.ec) { }
...
const color = this.avatarService.getRandomColor(name);

Should be (but not working):

import { AvatarService } from 'ngx-avatar';
...
constructor(private avatarService: AvatarService) { }
...
const color = this.avatarService.getRandomColor(name);

Did you imported the AvatarModule as:

import { AvatarModule } from 'ngx-avatar';

?

@odahcam Yes I did. I successfully integrated it as you can see below. Only problem is how to correctly call getRandomColor
image

Well, the AvatarService is not in the public API, so I'm afraid it is not intended to be used outside the lib itself. :/

Possibly. Anyway I upgraded from an older version of ngx-avatar where I had called it in a similar way like below:

import * as Avatar from 'ngx-avatar';
...
const color = Avatar.getRandomColor(name);

The reasoning is to retrieve the same color of my avatar in order to color e.g. a label I display next to it and wish to color same.

Well, in this case I think color could be a ngx-avatar component's output, so you could get the current value directly from the component.

Edit: yes, I checked and it does not exists in the component as well, but I think it would be a nice feature.

Thanks. So for now it's an enhancement request to the author.

Hi @bhalperin
FYI, I just published a new version of ngx-avatar which exports the service, defaultColors and defaultSources.

@HaithemMosbahi is this still open?

Nope, avatarService is now public and the requested method is API available.