-1

I am bad at explaining i hope you guys understand my requirements and help me out here.

I have a view with ng-repeat below

<div ng-repeat="item in allitems"> {{displaydata}} </div>

i have a controller with below data

$scope.allitems ={
1:{name:"car",price:"999",qty:8},
2:{name:"bag",price:"127",qty:2},
3:{name:"dog",price:"777",qty:3},
4:{name:"cat",price:"333",qty:4}
}
var displaychoice1 = "{{item.name}}" 
var displaychoice2 = "{{item.price}}"
$scope.displaydata = "I want " + displaychoice1 + " which cost " + displaychoice2 ;

I want users to be able to select the "fields" to output to the view instead of hardcoding what to display in the view.

2
  • well then just go with <div ng-repeat="item in allitems">I want {{item.name}} which cost {{item.name}}</div> Or what exactly do you need? Commented Sep 26, 2016 at 16:02
  • this ng-repeat wouldn't even work as written; this pattern is invalid for objects. allitems would have to be an array. Commented Sep 26, 2016 at 16:13

2 Answers 2

1

$scope.allitems should be an array

$scope.allitems = [
  {name:"car",price:"999",qty:8},
  {name:"bag",price:"127",qty:2},
  {name:"dog",price:"777",qty:3},
  {name:"cat",price:"333",qty:4}
]

And on the html you can simply

<div ng-repeat="item in allitems">
  I want {{item.name}} which cost {{item.price}}
</div>

EDIT

Allowing users to select fields could be something like

On controller:

$scope.displaydata = function(item, field1, field2){
  return "I want " + item[field1] + " which cost " + item[field2]; 
}

On HTML:

<div ng-repeat="item in allitems">
   {{ displaydata(item, 'name', 'price') }}
 </div>

Where you could adapt name and price to come from variables you have

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

1 Comment

Thanks so much, exactly what i am looking for
0

You can use ngoptions for it to allow user to select an option:

<select name="mySelect" id="mySelect"
      ng-options="item in allitems track by $index"
      ng-model="selectedOption">  </select>

I want {{selectedOption.name}} which cost {{selectedOption.price}}

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.