0

is it possible to use a predicate value for an ng-bind statement.

For example, I want to bind a td element that will change the property it displays dynamically.

<td ng-bind="user.month"></td

What if I wanted to change that element binding dynamically to

<td ng-bind="user.week"></td>

What would be the best way to achieve this. I'd have a select element that determines which predicate should be used for binding.

2 Answers 2

2

You can do this using [] object notation where you pass the variable bound to your select in the []

Assuming you have:

<select ng-model="models.selectVal">

And some data in scope like:

 $scope. data = {
    item1: 'Text in item 1',
    item2: 'Item 2 text'
  }

Then you can do:

 <div ng-bind="data[models.selectVal]"></div>

DEMO

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

1 Comment

Thank you. I did exactly this!
1

You can't do that just like that. There are two options that solve your problem. First (and the one that I recommend), don't change the binding. Make an extra field in your scope and change that field. Second option, you can change the ng-binding and then recompile the angular template, see Angular compile documentation

I hope this was helpfull. Good luck!!

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.