0
votes

I am a novice in angular js. I am creating some application where i am selecting a value from drop down list and the selected value is associated with ng-model. But i see that the ng-model is actually becoming null instead of gettting a value. My whole code is:

<html>

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
</head>
<body>
<div ng-app="app">
    <div ng-controller="MyCtrl">
        <h2>Select one Item from left and one Item from right</h2>
        <select ng-model="leftmodel"
                ng-change="ChangeOptions()"
                ng-options="option as option.value disable
  when option.disabled for option in left">
        </select>

        <select ng-model="rightmodel"
                ng-change="ChangeOptions()"
                ng-options="option as option.value disable when option.disabled for option in right">

        </select>
    </div>
</div>
<script>
    var module = angular.module("app", []);
    module.controller("MyCtrl",
            function($scope) {
                $scope.left = [{
                    "name": "apple",
                    "value": "Nice Apple",
                    "disabled": false
                }, {
                    "name": "orange",
                    "value": "Yellow Orange",
                    "disabled": true
                }, {
                    "name": "berry",
                    "value": "Blue Berry",
                    "disabled": false
                }];

                $scope.right = [{
                    "name": "apple",
                    "value": "Nice Apple",
                    "disabled": true
                }, {
                    "name": "orange",
                    "value": "Yellow Orange",
                    "disabled": false
                }, {
                    "name": "berry",
                    "value": "Blue Berry",
                    "disabled": false
                }];

                $scope.leftmodel = $scope.left[0];
                $scope.rightmodel = $scope.right[1];
                $scope.ChangeOptions = function() {
                    var size = $scope.left.length;
                    console.log(size);
                    console.log($scope.leftmodel);
                    console.log($scope.rightmodel);
                    for (var i = 0; i < size; i++) {
                        if ($scope.left[i].name === $scope.rightmodel.name) {
                            $scope.right[i].disabled = true;
                        } else {
                            $scope.right[i].disabled = false;
                        }
                    }

                    for (var i = 0; i < size; i++) {
                        if ($scope.right[i].name === $scope.leftmodel.name) {
                            $scope.left[i].disabled = true;
                        } else {
                            $scope.left[i].disabled = false;
                        }
                    }
                };

            }
    );
</script>
</body>
</html>

I also have a Codpen In console i get the following errors:

3 Object { $$hashKey: "object:5", disabled: false, name: "berry", value: "Blue Berry" } Object { $$hashKey: "object:7",
disabled: false, name: "orange", value: "Yellow Orange" } 3 null Object { $$hashKey: "object:7", disabled: true, name: "orange", value: "Yellow Orange" } "TypeError: Cannot read property 'name' of null at ChildScope.$scope.ChangeOptions (pen.js:87:58) at fn (eval at compile (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:15156:15), :4:159) at ChildScope.$eval (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:17972:28) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:25711:13 at Object. (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28536:9) at forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:20) at Object.$$writeModelToScope (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28534:5) at writeToModelIfNeeded (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28527:14) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28521:9 at validationDone (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28446:9)" 3 null null "TypeError: Cannot read property 'name' of null at ChildScope.$scope.ChangeOptions (pen.js:79:58) at fn (eval at compile (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:15156:15), :4:159) at ChildScope.$eval (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:17972:28) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:25711:13 at Object. (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28536:9) at forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:20) at Object.$$writeModelToScope (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28534:5) at writeToModelIfNeeded (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28527:14) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28521:9 at validationDone (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28446:9)" Do anyone have any idea why this is happening. What i want to do is select a value from left drop down menu and dont allowing the user to select the same value from other drop down list. That meansif a user selects some value from left select box he will not be able to select that from right select box and vice versa.

1

1 Answers

2
votes

Try this.

$scope.ChangeOptions = function() {

      var lsize = $scope.left.length;
      var rsize = $scope.left.length;
      for (var i = 0; i < lsize; i++) {

        console.log($scope.left[i]);
        if ($scope.left[i].name === $scope.rightmodel.name) {
          $scope.left[i].disabled = true;
        } else {
          $scope.left[i].disabled = false;
        }
      }

      for (var i = 0; i < rsize; i++) {
        if ($scope.right[i].name === $scope.leftmodel.name) {
          $scope.right[i].disabled = true;
        } else {
          $scope.right[i].disabled = false;
        }
      }
    };

Code Pen : https://codepen.io/anon/pen/dNYLpN