Trying to get this easy thing working but can't find it out...
I've got this url that looks like this:
https://website.com/image{width}x{height}.jpg
Now I need to replace the {width}
and the {height}
values.
I tried this function but I get the following error message all the time:
placeThumb(thumb:String){
thumb = thumb.replace('{width}', '300');
thumb = thumb.replace('{height}', '150');
return thumb;
}
The error code I'm getting is:
Uncaught (in promise): TypeError: Cannot read property 'replace' of undefined
TypeError: Cannot read property 'replace' of undefined at PlayerPage.webpackJsonp.109.PlayerPage.placeThumb (http://localhost:8100/build/main.js:202:23)
at Object.eval [as updateRenderer] (ng:///AppModule/PlayerPage.ngfactory.js:166:26)
at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:8100/build/vendor.js:15109:21)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14223:14)
at callViewAction (http://localhost:8100/build/vendor.js:14569:21)
at execComponentViewsAction (http://localhost:8100/build/vendor.js:14501:13)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14224:5)
at callWithDebugContext (http://localhost:8100/build/vendor.js:15472:42)
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:8100/build/vendor.js:15009:12)
at ViewRef_.detectChanges (http://localhost:8100/build/vendor.js:11993:22)
I think it should be really simple but maybe I am missing something.
HTML is looking as follows:
<ion-card>
<img [src]="placeThumb(playerData.twitch_thumb)"/>
<ion-card-content>
<ion-card-title>
<ion-icon name="logo-twitch" class="iconblink"></ion-icon> Streaming: {{playerData.twitch_title}}
</ion-card-title>
<p>
{{ playerData.twitch_thumb }}
{{ placeThumb(playerData.twitch_thumb) }}
</p>
</ion-card-content>
</ion-card>
thumb
is undefined. Where isthumb
coming from? – briosheje{{ placeThumb(playerData.twitch_thumb) }}
When I do it like the following, it does work. But it only replaced one value as you can see:placeThumb(thumb){ return thumb && thumb.replace('{height}', '150'); }
– JesseHplayerData.twitch_thumb
? Also, I personally would avoid to use a method in angular to display data. Either use a getter, either alter the value. If you're going to display many items with that criteria, you may encounter performance issues, since the value will be evaluated endlessly. – briosheje