1

Imagine I have the following HTML:

<p id="message" ng-show="something"></p>

Now during the lifetime of the page #message can be filled up with some text message after an AJAX call.

I would like #messageto toggle itself visible/hidden based on if it contains something or not.

Something like:

<p id="message" ng-show="self.text != ''"></p> <!--Not working but you get the idea -->
2
  • Instead of jQuery, can you populate the content of the paragraph with an AJAX call from an angular service? If so, you can define the result of the call as $scope.content and use the logic in Feussy's suggestion below. Commented Jun 19, 2014 at 19:16
  • Yeah I'm not using jQuery... I used "neutral" likd JS as pseudo-code in my example. Commented Jun 19, 2014 at 19:50

2 Answers 2

2

My best suggestion would be to bind the content of #message to a scope variable.

<p id="message" ng-show="Content != ''">{{Content}}</p>

Then you simply need to update the value of $scope.Content to change both the contents of #message and its visibility.

Sign up to request clarification or add additional context in comments.

Comments

1

Feussy is correct in that binding the #message element's content to a scope variable is the correct way to do this.

If for some reason you can't or don't want to bind the content to a variable in your Controller, you can bind the content directly on the element which will get you closer to the syntax that you are looking for.

<p id="message" ng-bind="self.text" ng-show="self.text !== ''"></p>

Here is a working example.

1 Comment

Thanks for the working example. My true problem was a $scope issue. I'm new to AngularJS and I put the <p> outside the element with ng-controller so it was out of scope...

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.