Javascript 如何根据angularjs onchange中下拉选项的选择值在div上添加动态id
这里我有一个选择框,一旦更改下拉列表,我需要根据下拉列表中所选选项的值在div上添加动态id。我在alert中提到了一旦更改要添加的id。下面是代码 htmlJavascript 如何根据angularjs onchange中下拉选项的选择值在div上添加动态id,javascript,html,angularjs,Javascript,Html,Angularjs,这里我有一个选择框,一旦更改下拉列表,我需要根据下拉列表中所选选项的值在div上添加动态id。我在alert中提到了一旦更改要添加的id。下面是代码 html 您可以在HTML中执行此操作: 您可以在更新功能中执行此操作: $scope.update = function() { if($scope.x == 'city'){ $scope.id = 'city1'; } if($scope.x == 'state'){ $scope.id =
您可以在HTML中执行此操作: 您可以在更新功能中执行此操作:
$scope.update = function() {
if($scope.x == 'city'){
$scope.id = 'city1';
}
if($scope.x == 'state'){
$scope.id = 'mystate';
}
if($scope.x == 'country'){
$scope.id = 'mycountry';
}
}
var app=angular.modulemyApp,[];
app.controllermyCtrl,函数$scope{
$scope.update=函数{
如果$scope.x==“城市”{
$scope.selectedValue='cityName';
}如果$scope.x=='state'{
$scope.selectedValue='stateName';
}如果$scope.x=='country'{
$scope.selectedValue='countryName';
}否则{
$scope.selectedValue=;
}
}
};
请选择
城市
州
国家
您已选择:-{selectedValue}
你有vlaue而不是ValueThank谢谢你的回答,问题解决了,现在我想根据所选选项在div中添加动态id。一旦更改,我更新了代码。错误。您不需要在回调中传递ngModel。它已经在范围内了,这是没有用的,因为这应该是一个评论,我理解,但我还没有能力在OP的帖子上发表评论。
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.update = function() {
if($scope.x == 'city'){
alert('add id as city1 inside ablove div');
}
if($scope.x == 'state'){
alert('add id as mystate inside ablove div');
}
if($scope.x == 'country'){
alert('add id as mycountry inside ablove div');
}
}
});
$scope.update = function() {
if($scope.x == 'city'){
$scope.id = 'city1';
}
if($scope.x == 'state'){
$scope.id = 'mystate';
}
if($scope.x == 'country'){
$scope.id = 'mycountry';
}
}
you need to use ngChange and pass ngModel object as argument to your ngChange function
**Example:**
<div ng-app="App" >
<div ng-controller="ctrl">
<select ng-model="blisterPackTemplateSelected" ng-change="changedValue(blisterPackTemplateSelected)"
data-ng-options="blisterPackTemplate as blisterPackTemplate.name for blisterPackTemplate in blisterPackTemplates">
<option value="">Select Account</option>
</select>
{{itemList}}
</div>
</div>
js:
function ctrl($scope) {
$scope.itemList = [];
$scope.blisterPackTemplates = [{id:1,name:"a"},{id:2,name:"b"},{id:3,name:"c"}];
$scope.changedValue = function(item) {
$scope.itemList.push(item.name);
}
}
Live example: http://jsfiddle.net/choroshin/9w5XT/4/