![](/img/trans.png)
[英]how to add dynamic id on div based on the value of selected option of dropdown in angularjs onchange
[英]How to get selected option value of dropdown in angularjs and change the div id dynamically onchange of dropdown
我正在使用angularjs。 我有一个选择框和来自ng repeat的div。在更改选择框时,我需要动态更改div id。例如 如果我的选项选择的值为city,则div id将再次为{{y.city}}。如果我的选项选择的值为state,则div id将为{{y.state}}。只需显示,我将id =“ { {y.state}}”在div中。 谁能帮我,这是下面的代码。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<select class="change" ng-model="x" ng-change="update()">
<option vlaue="city">Cities</option>
<option vlaue="state">States</option>
<option vlaue="country">Countries</option>
</select>
<div ng-repeat="y in details" id="{{y.state}}">
{{y.state}}
</div>
</div>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.details = [{"city":"city1","id":"1","state":"state1","country":"country1"},{"city":"city2","id":"2","state":"state2","country":"country2"},{"city":"city3","id":"3","state":"state3","country":"country3"}];
console.log($scope.details[0].city);
$scope.update = function() {
if($scope.x == 'city'){
alert('this is city');
}
if($scope.x == 'state'){
alert('this is state');
}
if($scope.x == 'country'){
alert('this is city');
}
}
});
修正错字vlaue
- > value
您的代码将是:
<div ng-repeat="y in details" id="{{ y[x] }}">
{{y[x]}}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.