AngularJS assign values to items in ng-repeat array using ng-model and retrieve those values -


this turning out lot harder expected.

i have 2 arrays: 1 products , 1 options. each product should have own array of options.

the problem starts when try give each product own options array own scope, meaning when 1 option selected pertains product's $scope , not other.

it suggested add [$index] array, doesn't seem work. can't retrieve specific values in array.

the options array has these ids: (name,price,active). before trying setting active boolean true , filtering results... either way fine, in nutshell need retrieve name , price values of chosen option...

this wasn't easiest explain bear me... looking.

option view html

<md-card ng-repeat="item in items.results | filter:true">      <img ng-src="{{item.img}}"            class="md-card-image"            alt="">      <md-card-content class="content">           <h2 class="md-title">{{ item.name }}</h2>           <h4>{{ item.price | currency }}</h4>           <md-list>               <p class="md-subhead">choose flavor</p>               <md-divider></md-divider>               <md-list-item ng-repeat="option in options.results"                              layout="row">                   <p> {{ option.name }} </p>                   <span flex></span>                   <p> {{ option.price | currency}} </p>                   <md-checkbox aria-label="option.active"                                class="md-accent"                                 ng-model="item.flavor[$index]">                   </md-checkbox>               </md-list-item>           </md-list>        </md-card-content>        <md-action-bar layout="row"                        layout-align="end center">            <md-button class="md-fab md-accent fab"                        aria-label="remove cart"                        ng-click="remove(item)"                                                      ng-class="{active:item.active}">                <md-icon md-svg-src="img/icons/remove.svg"></md-icon>            </md-button>        </md-action-bar>  </md-card> 

here order html trying use values..

<md-card>     <md-card-content>         <md-list>             <span class="md-subhead">review order</span>             <md-divider></md-divider>             <md-list-item ng-repeat="item in items.results | filter:true"                            layout="row">                 <span>{{ item.name }}</span>                 <span flex></span>                 <span>{{ item.price | currency}}</span>                 <span ng-repeat="flavor in item.flavor | filter:true">                     {{flavor}}                 </span>                       </md-list-item>             <md-divider></md-divider>             <md-list-item layout="row">                 <span>order total :</span>                 <span flex></span>                 <span>{{ total(items.results) | currency}}</span>             </md-list-item>         </md-list>        </md-card-content> </md-card> 

instead of trying use ng-model directive can create following functions in controller

$scope.addoption = function(item, opt) {     var index = item.flavors.indexof(opt);     if(index > -1) {         item.flavors.splice(index, 1);     }     else         item.flavors.push(opt); }  $scope.checkoption = function(item, opt) {     return item.flavors.indexof(option) > -1 }    

and change html to

<md-checkbox aria-label="option.active"            class="md-accent"              ng-checked="checkoption(item, option)"            ng-click="addoption(item, option)"> </md-checkbox> 

you can access flavors follows

<div ng-repeat="flavor in item.flavors">[{{flavor.name}} : {{flavor.price}}] </div>  

you can test idea here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -