0

I am working in app and i get this complicated situation with select and the select value. I have a json answer "portions" if it is necessary i can re-arrange my json to solve my problem.

"products": [
    {...},
    {...},
    {
        // Here the json
        "id": 13,   
        "hightlighted": true,   
        // Here the portions that i loop in the "select"
        "portions": [
            {
              "size": "De 10 a 20 porciones",
              "price": "20"
            },
            {
              "size": "De 20 a 30 porciones",
              "price": "30"
            },
            {
              "size": "De 30 a 40 porciones",
              "price": "40"
            }
        ]
    }
]

<div class="item" ng-repeat="product in products">
// Here is my try one
<select class="form-control" ng-init="size=product.portions[0].size" ng-model="size" >
    <option ng-repeat="p in product.portions" value="[[p.size]]" > 
        [[p.size]]
    </option>
</select>
<div>
    [[I need here the selected PRICE]] <br>
    [[size]] <br>
</div>

// My try 2
<select class="form-control" ng-init="price=product.portions[0].price" ng-model="price" >
    <option ng-repeat="p in product.portions" value="[[p.price]]" > 
        [[p.size]]
    </option>
</select>
<div>
    [[price]] <br>
    [[I need here the selected SIZE]]
</div>
</div>

Thank you for any help.

edit. I am using [[]] instead of {{}}

.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});

Here the solution !!! thanks solution

9
  • You should be using {{ }} for data binding, not [[ ]]. See the docs for select here: https://docs.angularjs.org/api/ng/directive/select Commented Oct 17, 2015 at 17:20
  • @buzzsaw i am using [[ ]] instead of {{ }}. Commented Oct 17, 2015 at 17:22
  • Can you also add your controller and where is it in html? Commented Oct 17, 2015 at 17:26
  • Didn't realize you had modified the provider. Commented Oct 17, 2015 at 17:26
  • 1
    Your question would be easier to answer if you told use what you want to achieve. What do you want the end result to be? What do you want the select box to look like? Commented Oct 17, 2015 at 17:30

2 Answers 2

1

You are looking for ngOptions.

Given that I understand your case, select as label for value in array is the correct approach.

<select ng-options="portion.price as portion.size for portion in portions">
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer same as mine but more concise.
Thank you @KGChristensen It helped me too to solve my problem, here the solution solution
0

Better I think to use ng-options. Ng-init should only be used for ng-repeat aliasing.

angular.module('myApp',[])
.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
})

.controller('MyController', function($scope){

  $scope.product = {
"id": 13,   
"hightlighted": true,   
// Here the portions that i loop in the "select"
"portions": [
    {
      "size": "De 10 a 20 porciones",
      "price": "20"
    },
    {
      "size": "De 20 a 30 porciones",
      "price": "30"
    },
    {
      "size": "De 30 a 40 porciones",
      "price": "40"
    }
]
    }
  
  $scope.product.selectedPortion = $scope.product.portions[0];
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>

<div ng-app='myApp' ng-controller="MyController">
<select class="form-control" ng-options="p as p.size for p in product.portions" ng-model="product.selectedPortion" >
</select>
<div>
    [[product.selectedPortion.price]] <br>
</div>
</div>

1 Comment

thank you, i made it on code pen and works, also i made a little change link

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.