angularjs - Update data from drop down list -
how can update content of element when selection made drop down list?
i want update:
- the description fields,
- price , id in block details.
here link of plunker created code (must manually navigate version dated jun 13, 2015 2:49:35 pm
), in following snippet.
(function() { 'use strict'; var app = angular.module('testapp', []); app.controller('testctrl', ['$scope', function($scope) { $scope.variants = [{ 'id': 'p-12345', 'url': 'section/food/product/p-12345', 'title': 'product 1', 'price': '£21.15 - 21x15', 'description': 'lorem ipsum dolor' }, { 'id': 'p-12366', 'url': 'section/food/product/p-12366', 'title': 'product 2', 'price': '£11.15 - 10kg', 'description': 'lorem ipsum dolor' }, { 'id': 'p-12364', 'url': 'section/food/product/p-12364', 'title': 'product 3', 'price': '£67.15 - 60kg', 'description': 'lorem ipsum dolor' }]; }]); })(window, angular);
<!doctype html> <html ng-app="testapp"> <head> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="style.css" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div class="container"> <h4>select 1 product!</h4> <div class="row" ng-controller="testctrl"> <div class="col-md-4"> <div class="form-group"> <select id="selectvariant" ng-model="selectitem" class="form-control"> <option selected="selected" disabled="disabled">choose 1 product...</option> <option ng-repeat="variant in variants" value="{{variant.url}}">{{variant.title}}</option> </select> </div> <input type="hidden" value="{{variants.id}}"> <div class="form-group"> <button class="btn btn-danger">add cart</button> </div> </div> <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-body"> <h4>details:</h4> <p>price: {{selectitem.variants.price}}</p> <p>description: {{selectitem.variants.description}}</p> <p>id product: {{selectitem.variants.id}}</p> </div> </div> </div> </div> </div> </body> </html>
problem solved!!! :) :)
i've updated code in plunker... in case has same problem.
i had change this:
<div class="form-group"> <select id="selectvariant" ng-model="selectitem" class="form-control"> <option selected="selected" disabled="disabled">choose 1 product...</option> <option ng-repeat="variant in variants" value="{{variant.url}}">{{variant.title}}</option> </select> </div>
to this:
<div class="form-group"> <select id="selectvariant" ng-model="productselect" ng-options="product product.price product in variants track product.url" class="form-control"> <option selected="selected" disabled="disabled">choose 1 product...</option> </select> </div>
(function() { 'use strict'; var app = angular.module('testapp', []); app.controller('testctrl', ['$scope', function($scope) { $scope.variants = [{ 'id': 'p-12345', 'url': 'section/food/product/p-12345', 'title': 'product 1', 'price': '£21.15 - 21x15', 'description': 'lorem ipsum dolor' }, { 'id': 'p-12366', 'url': 'section/food/product/p-12366', 'title': 'product 2', 'price': '£11.15 - 10kg', 'description': 'lorem ipsum dolor' }, { 'id': 'p-12364', 'url': 'section/food/product/p-12364', 'title': 'product 3', 'price': '£67.15 - 60kg', 'description': 'lorem ipsum dolor' }]; }]); })(window, angular);
<!doctype html> <html ng-app="testapp"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" /> <link rel="stylesheet" href="style.css" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div class="container" ng-controller="testctrl"> <h4>select 1 product!</h4> <div class="row" > <div class="col-md-4"> <div class="form-group"> <select id="selectvariant" ng-model="productselect" ng-options="product product.price product in variants track product.url" class="form-control"> <option selected="selected" disabled="disabled">choose 1 product...</option> </select> </div> <input type="hidden" value="{{productselect.id}}"> <div class="form-group"> <button class="btn btn-danger">add cart</button> </div> </div> <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-body"> <h4>details:</h4> <p>price: {{productselect.price}}</p> <p>description: {{productselect.description}}</p> <p>id product: {{productselect.id}}</p> </div> </div> </div> </div> </div> </body> </html>
Comments
Post a Comment