2

I want to make a dynamic page, that depending of some aspects shows one list or another list, both arrays have different variable names. I tried to use somethings to put dynamic ng-repeat expression but without success.

Anyone has some suggestion? I made a fiddle to explain what i need.

<div ng-repeat="item in itemsToShow" >
    <!--THIS IS THE EXPRESSION THAT I WANT DYNAMIC-->
 {{item.name}}, {{item.title}}

[fiddle] http://jsfiddle.net/eTTZj/867/

6
  • What are you trying to achieve? Commented Jul 17, 2014 at 9:58
  • I'm struggling to understand what you want to do. What's wrong with the fiddle? Commented Jul 17, 2014 at 9:58
  • With "dynamic" you mean what? Commented Jul 17, 2014 at 9:58
  • He means dynamic because fields have different names in the two lists. ( title versus titleBig for instance) Commented Jul 17, 2014 at 10:04
  • I understand now. There's no concrete answer for this situation, I would advise you to either organise your data homogenously, or if they are semantically different, create a template for each and use an ng-switch to determine which template to use Commented Jul 17, 2014 at 10:13

2 Answers 2

0

I've updated your JSFiddle. When you want to be 'dynamic', just code the logic inside the controller and set the $scope.itemsToShowwith different arrays, so your view hasn't to change.

Like this:

....
var show_items_2 = true;
/*Whatever you want to do here*/
if(show_items_2) {
   $scope.itemsToShow = items2;
} else {
    $scope.itemsToShow = items;
}

When you want to have different keys inside your array ('name' and 'nameBig'), you have to access the items differently (through (key, value)). See this SO question here.

<div ng-repeat="item in itemsToShow"></div>
<div ng-repeat="(key, value) in item">
    {{key}} : {{value}}
</div>

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

Comments

0

You could use operators :

    <div ng-repeat="item in itemsToShow" >
     {{item.name || item.firstname}},  {{item.title || item.titleBig}}
   </div>

This will pick the existing property in your list.

If it's unsunstainable for many, many lists you can also do this:

<div ng-repeat="item in itemsToShow"></div>
    <div ng-repeat="(key, value) in item">
        {{key}} : {{value}}
    </div>
</div>

1 Comment

Let me explain in a better way. I have and app with 30 input fields. In each field i want a modal with the list of every possible values for that field. but that information comes from a different array with different keys (email,name,phone) for every field and I am trying to make a generic modal to every input field

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.