I am pretty new to angularJS
. I am trying to create an interface for the user where he will be able to select exactly two items from two drop down list. They are actually the same options. When the user selects some option from the left drop down list he will not be able to select the same thing from the right drop down list and vice versa. The user will also have a button to add more options. The code works fine for everything but when the user clicks the button to add more options the newly added options doesn't work at all.
Here is my full code:
<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>
<button ng-click="addOptions()">Click Me</button>
</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.addOptions=function()
{
var banana={
"name": "banana",
"value": "Tasty Banana",
"disabled": false
};
var orange={
"name":"orange",
"value":"Soour Orange",
"disabled":false
};
$scope.left.push(banana);
$scope.left.push(orange);
$scope.right.push(banana);
$scope.right.push(orange);
};
$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;
}
}
};
}
);
</script>
</body>
</html>
I also have a Codepen. The errors i get once clicking a newly created option on clicking the button are:
TypeError: Cannot read property 'name' of null at ChildScope.$scope.ChangeOptions (first.html:89) at fn (eval at compile (angular.js:15156), :4:159) at ChildScope.$eval (angular.js:17972) at angular.js:25711 at Object. (angular.js:28536) at forEach (angular.js:357) at Object.$$writeModelToScope (angular.js:28534) at writeToModelIfNeeded (angular.js:28527) at angular.js:28521 at validationDone (angular.js:28446)
Can anyone help me, how can i solve this problem? Thanks in advance.
I am noticing one more strange thing. It can console the names at line 88. But it fails at the same line saying it cant find name
of null
at line 88. The image Strange Bug shows the scenario.
var rsize = $scope.left.length;
why would the size of the right be the length of the left? This probably worked as long as the arrays were the same size, but the moment something is added to one side and not the other, this is going to break.