3

I'm doing an AngularJS application and I end up with this problem:

I have some text that it has to be dynamic... the text will be reacheable in the following variable:

"scope.text.text_id"

The problem is to set this id variable with angular's {{}}

I can't use as an array instead of an object because i have tons of empty ids so it would be most of it useless

Any ideas?

Thank in advice to anyone

2 Answers 2

4

Sure, this is just plain javascript. You can set a property on an object using the [] operator on the object.

The code for angular would be something like this:

In the controller:

$scope.id = "myprop";
$scope.text = {};
$scope.text['text_' + $scope.id] = "myValue";

And in the directive:

{{text["text_" + id]}}

I made this plunker showing the behaviour. http://plnkr.co/edit/72mHY2AxQgwqTuXX9Myd

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

Comments

3

Providing that you have scope property id you can use bracket notation like this to access dynamic text from object scope.text:

{{ text['text_' + id] }}

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.