如果从下拉列表中进行选择,如何更新其他元素的内容?
我想更新:
以下是我使用代码创建的plunker的链接(必须手动导航到Jun 13, 2015 2:49:35 PM
的版本),该链接也在以下代码段中。
(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>
答案 0 :(得分:1)
问题解决了!!! :) :)
我已经更新了Plunker中的代码......万一有人遇到同样的问题。
我只需要改变这个:
<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>
对此:
<div class="form-group">
<select id="selectVariant" ng-model="productSelect" ng-options="product as product.price for product in variants track by product.url" class="form-control">
<option selected="selected" disabled="disabled">Choose one 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 one product!</h4>
<div class="row" >
<div class="col-md-4">
<div class="form-group">
<select id="selectVariant" ng-model="productSelect" ng-options="product as product.price for product in variants track by product.url" class="form-control">
<option selected="selected" disabled="disabled">Choose one product...</option>
</select>
</div>
<input type="hidden" value="{{productSelect.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: {{productSelect.price}}</p>
<p>Description: {{productSelect.description}}</p>
<p>ID Product: {{productSelect.id}}</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>