How can I update the content of another element when a selection is made from a drop down list?
I want to update:
- the description fields,
- price and ID in the block details.
Here is the link of plunker I created with the code (must manually navigate to version dated Jun 13, 2015 2:49:35 PM
), which is also in the 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 one 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 one 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 to 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 the code in Plunker... In case anyone has the same problem.
I only had to change this:
To this: