0

I am trying to hide a span if the context of the span is empty. However, the content of the span is some complicated calculation. What I want to do, is exactly below, however, I do not want to call the function twice. The element below is also in a "Loop" (ASP.NET MVC foreach in the razor page :( ).

Here is is

@for(...) {
<span ng-hide="GetDataCount() > 0">{{GetDataCount()}}</span>
}

As you can see, this will work, but I have to call the function 2 times. Any ideas how I can make this work if the "span" element we're currently in doesn't have a value? (i.e. if GetDataCount returns null or 0).

Thanks!

1 Answer 1

2

You can use ng-init to set a value:

<span ng-init="count = GetDataCount()" ng-hide="count > 0">{{count}}</span>
Sign up to request clarification or add additional context in comments.

3 Comments

Please note the warning in the angular docs: The only appropriate use of ngInit is for aliasing special properties of ngRepeat.
Wouldn't this case be similar to the ng-init example? Aliasing "count" as GetDataCount() result?
@Dave -- It's a bit of a special case since this is residing within a server-side repeater

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.