0

I am creating a list using ng-repeat like this:

<li ng-repeat="(key, value) in Myctrl.data"> {{Myctrl.resources.key}}</li>

Myctrl.data will look somthing like this:

{
personal : 
    {a: 100, b: 120}, 
professional : 
    { a: 320, b : 410}
}

Now in MyCtrl I have some mappings in resources object for each of the keys obtained from ng-repeat and I want to get the values of these keys and put it in list items.

$scope.resources = {
    personal: "Personal Details",
    professional: "Professional Details",
    freelance: "Freelance Details",
    custom: "Custom Details"
}

So when my ng-repeat is executed, I will get personal, professional etc and I want to substitute the values from resources object in my HTML.

How can I replace key in {{Myctrl.resources.key}} with actual value of key obtained in ng-repeat.

1
  • What keys does Myctrl.data contains? and can u explain what you exactly need? Commented Sep 20, 2016 at 11:48

2 Answers 2

1

Try this...

<li ng-repeat="(key, value) in Myctrl.data">{{Myctrl.resources[key]}}</li>
Sign up to request clarification or add additional context in comments.

2 Comments

I am not looking for this. I want to take the key and access a value from resources object which has the same key.
Sry, my bad! Try the below one... <li ng-repeat="(key, value) in Myctrl.data">{{Myctrl.resources[key]}}</li>
0
<li ng-repeat="(key, value) in Myctrl.data"> {{resources.key}}</li>

will produces output list items as

Personal Details
Professional Details

Because in your repeat will produces

firsttime key is personal 
    {{resources.personal}} => Personal Details
secondtime key is professional
    {{resources.professional}} => Professional Details

This is how it works exactly, what you actually looking for is this only?

3 Comments

Did you even test this code? This is exactly what I have written. It doesn't work.
Yeah it wont work that's why i changed it {{Myctrl.resources.key}} to {{resources.key}}
Its an instance variable from controller. There is nothing as resources. We can actually access using keys instead of dot notation. That does the trick.

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.