1

I have two select option and am using ng-options to get the data from json files. My concern is if a value is selected in first select, i need to automatically choose a corresponding value in the other select. I am able to populate the data in the dropdowns. but am unable to connect the two. Please help.

Below is my code for first select:

<select ng-model="firstType"
                            ng-change="firstTypeChange()"
                            ng-options="firstValue as firstValue.value group by firstValue.group for firstValue in firstList" tabindex="1">
                            <option value=""></option>
                    </select>    

And the second select is as follows:

<select ng-model="secondType"
                            ng-change="secondTypeChange()"
                            ng-options="secondValue as secondValue.value group by secondValue.group for secondValue in secondList"
                            tabindex="2">
                            <option value=""></option>
                    </select>    

And this is my code from controller:

$scope.firstType = "";
$scope.secondType = "";
$scope.isFirstTypeShow = true;
$scope.isSecondTypeShow = true;

$scope.firstTypeChange = function() {
$scope.formType = $scope.firstType.formType;
 if ($scope.firstType.formType == 'inquiry') {
   $scope.isSecondTypeShow = false;
 } else if ($scope.firstType.formType != 'inquiry' || $scope.firstType.formType == null) {
   $scope.isSecondTypeShow = true;
 } else if ($scope.formType == 'known'){
   $scope.secondType.formType = 'existing';
 }
}
$scope.secondTypeChange = function() { $scope.secondFormType = $scope.secondType.formType;};
4

2 回答 2

1

您的第二个 if 语句有问题

if ($scope.firstType.formType != 'inquiry' || $scope.firstType.formType == null) {

你已经从第一个 if 语句中知道这个 $scope.firstType.formType != 'inquiry' 是真的,这意味着这个 if 语句总是真的,最后一个 if 语句永远不会到达。

最后一个 if 仍然不起作用,您需要将 secondList 中的值分配给 $scope.secondType 以选择该值。在这里查看JSFiddle

$scope.firstTypeChange = function () {
    $scope.formType = $scope.firstType.value;   
    if ($scope.formType == 'inquiry') {
        $scope.isSecondTypeShow = false;
    } else if ($scope.formType == null) {
        $scope.isSecondTypeShow = true;
    }
    else if ($scope.formType == 'known') {
        $scope.secondType = $filter('filter')($scope.secondList, {value:'existing'})[0];
        $scope.isSecondTypeShow = true;
    }
    else {
        $scope.isSecondTypeShow = true;
    }
}
于 2014-08-20T18:57:01.453 回答
0

尝试在第一个选择项目上添加手表。然后根据第一项中选择的值设置第二项的默认值。让我知道您是否为此提供了 jsfiddle 示例

于 2014-08-20T17:11:14.397 回答