1

I have an angular service that returns a list of items from the database.

I display those items through an ng-repeat. I'd like to hide/show each one of them using the ng-show.

Is it a good practice to add a "display" property directly on my items to show or hide them in the UI?

Edit: If someone could point me to an article explaining that orientation (can't seem to find any).

2
  • Why would you add displaywhen you're already using ng-show? Commented Sep 29, 2016 at 18:54
  • it's certainly easiest to add a property as needed...especially if the data is read only. depends on if you need to return that data to server also, or if you need storage persistence. A filter would be more efficient than ng-show though Commented Sep 29, 2016 at 18:55

2 Answers 2

1

Yes. This is the right choice. It enables your model to control how items are viewed according to a separately controlled logic. This makes your application scalabale too.

A filter is a better choice in further modeling your logic.

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

2 Comments

about doing it through expressions in the view what do you mean exactly? Could you give me a brief example please?
I think I must correct myself there. What I meant was scope variable which were never referenced in the controller.
1

As suggested by other answers, filter is the better choice for your case. Add a property display and then filter based on that property.

 <div ng-repeat="item in dataFromServer |  filter:{ display: true }">
  {{item.name}}
  </div>

I have used underscore to create a new property for each object

https://jsfiddle.net/k8u3c8t7/

Comments

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.