Я пытаюсь заполнить ссылку с помощью выбранного варианта в раскрывающемся списке. Я не знаю, как получить значение из опции about и не повторять его.
<div ng-app="" ng-init="horsepower=[
{hp:'15',value:'https://www.compressorworld.com/15-hp-rotary-screw-air-compressor-system-230-3-60-total-air-system.html'},
{hp:'20',value:'https://www.compressorworld.com/20-hp-rotary-screw-air-compressors-complete-air-system-lifetime-airend-warranty.html'}]">
<select>
<option ng-repeat="x in horsepower" value="{{ x.value}}" ng-model="hp">{{ x.hp}}</option>
</select>
<br/><br/>
<!-- get the selected value of select -->
<a ng-href="{{}}">
buy
</a>
</div>
Лучше использовать ng-options
в теге select
и использовать ng-model
для вызова выбранного значения в href
<select ng-options="x as x.hp for x in horsepower" value="{{ x.value}}" ng-model="hp">
попробуй это.
var app = angular.module("app",[]);
app.controller("ctrl" , function($scope){
$scope.test = function(value){
$scope.href= value;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" ng-init="horsepower=[
{hp:'15',value:'https://www.compressorworld.com/15-hp-rotary-screw-air-compressor-system-230-3-60-total-air-system.html'},
{hp:'20',value:'https://www.compressorworld.com/20-hp-rotary-screw-air-compressors-complete-air-system-lifetime-airend-warranty.html'}]">
<select ng-model="hp" ng-options="x.value as x.hp for x in horsepower" ng-change="test(hp)"></select>
<br/><br/>
<!-- get the selected value of select -->
<a ng-href="{{href}}">
buy
</a>
</div>
поэтому в основном вы поместили ng-модель в неправильное место, вы должны поместить ее на выбор, а не на опцию. кроме этого, вы должны использовать переменную hp внутри href, и вот она там!
<div ng-app="" ng-init="horsepower=[
{hp:'15',value:'https://www.compressorworld.com/15-hp-rotary-screw-air-compressor-system-230-3-60-total-air-system.html'},
{hp:'20',value:'https://www.compressorworld.com/20-hp-rotary-screw-air-compressors-complete-air-system-lifetime-airend-warranty.html'}]">
<select ng-model="hp">
<option ng-repeat="x in horsepower" value="{{ x.value}}">{{ x.hp}}</option>
</select>
<br/><br/>
<!-- get the selected value of select -->
<a href="{{hp}}">
buy {{hp}}
</a>
</div>
вы можете узнать больше об угловых и выбрать здесь - https://docs.angularjs.org/api/ng/directive/select
Как и выше, но немного чище
<div ng-app="" ng-init="horsepower=[
{hp:'15',value:'https://www.compressorworld.com/15-hp-rotary-screw-air-compressor-system-230-3-60-total-air-system.html'},
{hp:'20',value:'https://www.compressorworld.com/20-hp-rotary-screw-air-compressors-complete-air-system-lifetime-airend-warranty.html'}]">
<select ng-options="x.hp for x in horsepower" ng-model="hp"></select>
<br/><br/>
<!-- get the selected value of select -->
<a ng-href="{{hp.value}}">
buy
</a>
</div>
hp=horsepower[0]
в конце вашего ng-init
Рабочая скрипка.
Используйте ngOptions
вместо ngRepeat
и используйте ngModel
в теге select
.