Get value of ng-init

Tags: #<Tag:0x00007fe052489718>

(Johan Stigert) #1

Not sure if I am doing this correctly but I am receiving an image with “ng-init”. Now I want to display the image only if there is an image to be displayed.
Custom Widget:

<div ng-init="model_2017 = { 'url':'', 'refresh': 5 }">
<widget-image ng-if="model_2017 != ''" ng-model="model_2017" style="margin:auto;"/>

In the code above the “ng-if” are not working. Is there any way to check the value for “model_2017”? I am returning an empty String from the server with a Statuscode 202 (no content).

If this needs to be done in another way(controller, script file, items etc), then please point me in the right direction.

(Rich Koshak) #2

I can’t help but I moved this to the HABPanel category where hopefully someone who can help will be more likely to see it.

(Lucky) #3

Your ng-if will never be false since you are already initializing it with ng-init. Can you post your entire code

(Johan Stigert) #4

That is the entire code. But can maybe make it more understandable by adding one more image:

<div ng-init="model_2017 = { 'url':'', 'refresh': 5 }">
<widget-image ng-if="model_2017 != ''" ng-model="model_2017" style="margin:auto;"/>

<div ng-init="model_2018 = { 'url':'', 'refresh': 5 }">
<widget-image ng-if="model_2018 != ''" ng-model="model_2018" style="margin:auto;"/>

Would it be better to make the images as String “items” instead with Http binding and then use “ng-if” on the String items?

(Lucky) #5

As I said, your ng-if will never be false because by the time ng-if is hit, you’ve already set model_2017 to something.

The widget-image does not take into account if the image exists or not. It will still show the widget, with a blank/missing (x) image if the image does not exist.

If you want to use a string item, your ng-init must be:

ng-init="model={url: itemValue('xxx'))"
and your ng-if will be


(Johan Stigert) #6

Thanks. I will try solving this with url as “items” instead.