0

I have a requirement – single angular js function to return the scope variable dynamically based on one of the input parameter to the function. For controller function, I found some example on how to return dynamic scope like

$scope[attributeId] = data.response; (attributeId is an input parameter, data.response is an array)

Question is how to use such scope variable in HTML? I have a select control like this which will populate a dropdown with the values retuned in the scope, what should be specified as model and option in such case.

<button ng-click="getAttributeValue(getProductsModel,p.attributeId, $event)">Get Attribute Value</button>
<select data-ng-model="attributeModel" 
    data-ng-options="a for a in attributeResponse">
    <option value="">-- Select Value --</option>
</select>

An example would really help.

1 Answers1

0

You should assign it to $scope.attributeId. After that, you can populate it using the ng-options directive.

Now I don't know how your data structure is, so here's an example with cars.

The imaginary array:

$scope.cars = [
    {
        id: 1,
        name: 'Ford'
    },
    {
        id: 3,
        name: 'Mercedes'
    },
    {
        id: 3,
        name: 'BMW'
    },
]

And the <select> element with the populated cars:

<select ng-model="selectedOption" ng-options="car.name for car in cars">

Now you can access the chosen option using $scope.selectedOption.

Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
  • Not exactly. The same function will be called multiple time with different input, each time it will return an array with different data which need to persisted. Each scope like $scope[101], $scope[201], $scope[301] to be bound to different dropdown. – user2596838 Feb 11 '15 at 13:00