Ray Suelzer
Ray Suelzer

Reputation: 4107

Using ng-bind with a variable predicate

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.

Upvotes: 0

Views: 58

Answers (2)

charlietfl
charlietfl

Reputation: 171679

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

Upvotes: 2

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!!

Upvotes: 1

Related Questions