I have created a table with a row that has three dropdowns
Drug - Dose - Period
I also created a button Add Drug that will add another row of the same dropdowns
I am getting the data from a database and populating it in the dropdown
Exception:
The dose dropdown changes depending on what the drug is. So I created a watcher to check when the value of the drug changes, and then created an array of the dose data called $scope.array and populated it in $scope.selectedDose which are the values inside the dose dropdown. So the first drug dose data is different than the second drug dose data. Please select the second drug and 5th drug from dropdown in jsfiddle and see how dose values changes
$scope.$watch("value", function() {
$scope.array = $scope.value.dose_array.split(',');
$scope.selectedDose = $scope.array[0];
});
Problem:
If you play around with my JSFiddle link that I shared, you will find that when adding a drug with the button and selecting another drug from the first, the dose data of the first row changes too. The reason for that is because $scope.selectedDose is changing with any drug data from any row
The solution that I thought of is creating an array of an array
$scope.selectedDose[rowIndex] = $scope.array[0];
with every row added, the dose dropdown will have its own selectedDose data. However it was complicated and was not able to accomplish it
Any solutions for this problem ? I have added the JSFiddle and organized my code as much as possible. It should demonstrate my problem very well
JSFIDDLE
TypeError: Cannot read property 'dose_array' of nullat$scope.array = $scope.value.dose_array.split(',');addRowfunction is copying the first element of the array ($scope.rows[0]). Try creating an object you can clone from outside the array so that when you set the first row to a value, it doesn't cascade.