Javascript 角度-尝试同步两个下拉列表
我正在尝试同步Angular 1.4.7中的两个下拉列表,以便在更新一个下拉列表时,将另一个下拉列表设置为相同的值,反之亦然。看起来im已关闭,但另一个下拉列表没有同步,而是更改为默认的“选择”值Javascript 角度-尝试同步两个下拉列表,javascript,angularjs,Javascript,Angularjs,我正在尝试同步Angular 1.4.7中的两个下拉列表,以便在更新一个下拉列表时,将另一个下拉列表设置为相同的值,反之亦然。看起来im已关闭,但另一个下拉列表没有同步,而是更改为默认的“选择”值 <div class="form-group" ng-repeat="input in inputData"> <select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
这是我正在处理的一个网站上的一个问题的简化版本。讨论中的站点是非常数据驱动的,所以我希望能够在这个结构中实现这一点。也就是说,inputData对象的数组,每个对象都包含月份数组&selectedMonth属性
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
除此新要求外,该应用程序工作正常。我错过了什么
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
我包括了来源和一个显示问题的plunker
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
HTML
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
挑选
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
控制器-test.js
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
'use strict';
var app = angular.module('app',[]);
app.controller('testController', ['$scope', function ($scope) {
$scope.inputData=[];
$scope.inputData.push(new inputData());
$scope.inputData.push(new inputData());
$scope.option_changed = function(Input) {
SyncDropdowns(Input)
};
function SyncDropdowns(selectedInput){
for(var i = 0; i < $scope.inputData.length ; i++) {
var currInput=$scope.inputData[i];
if (currInput!=selectedInput){
currInput.selectedMonth=$.extend({}, selectedInput.selectedMonth);
}
}
}
function inputData(){
this.selectedMonth={};
this.Months=["April", "May", "June", "July"];
}
}]);
“严格使用”;
var-app=angular.module('app',[]);
app.controller('testController',['$scope',函数($scope){
$scope.inputData=[];
$scope.inputData.push(newinputdata());
$scope.inputData.push(newinputdata());
$scope.option_changed=函数(输入){
同步下拉列表(输入)
};
功能同步下拉列表(selectedInput){
对于(变量i=0;i<$scope.inputData.length;i++){
var currInput=$scope.inputData[i];
if(currenput!=selectedInput){
currInput.selectedMonth=$.extend({},selectedInput.selectedMonth);
}
}
}
函数inputData(){
this.selectedMonth={};
月数=[“四月”、“五月”、“六月”、“七月”];
}
}]);
不确定您打算在这里用
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
currInput.selectedMonth=$.extend({},selectedInput.selectedMonth)
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
我将其更改为以下内容,并且可以很好地同步
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
currInput.selectedMonth=selectedInput.selectedMonth;
下面是jquery扩展的
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
$.extend({},"June")
返回
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
{
"0": "J",
"1": "u",
"2": "n",
"3": "e"
}
更新此函数
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>
function SyncDropdowns (selectedInput) {
for(var i = 0; i < $scope.inputData.length ; i++) {
var currInput=$scope.inputData[i];
if (currInput!=selectedInput){
currInput.selectedMonth=selectedInput.selectedMonth;
}
}
}
功能同步下拉列表(selectedInput){
对于(变量i=0;i<$scope.inputData.length;i++){
var currInput=$scope.inputData[i];
if(currenput!=selectedInput){
currInput.selectedMonth=selectedInput.selectedMonth;
}
}
}
在ng repeat中,每个实例都有自己的作用域。尝试使用
$parent.input。在ng model
<div class="form-group" ng-repeat="input in inputData">
<select name="inputMonth__{$index}}" id="inputMonth__ID_{{$index}}"
ng-model="input.selectedMonth"
ng-change="option_changed(input)"
ng-options="month as month for month in input.Months " >
<option value="" style="display:none">Select</option>
</select>
<br><br>
</div>