I am facing an issue with the data
in my controller
$scope.data = {
home: {
baseValue: "1",
name: "home"
},
contact: {
baseValue: "2",
name: "contract"
}
// numerous other options
};
How can I simplify the process of determining when the baseValue
changes without having to loop through all the arrays?
Here is a snippet of my HTML:
<section class="content row" ng-repeat="item in data">
{{item.name}}
....
</section>
I have tried using a general $watch function, but it requires me to iterate through each object individually.
$scope.$watch('data', function (newValue, oldValue, scope) {
// code for comparing the arrays line by line
}, true);
Any suggestions on how to implement a more efficient $watch to only track changes in the baseValue
property?
For related queries, check out these links:
- AngularJS watch array of objects for data change
- How to get an object that was changed in angularjs?
- How to deep watch an array in angularjs?
UPDATE 1
If there are multiple objects in the data set, adding individual $watch functions for each object could become messy and inefficient. Any suggestions on improving this process?
$scope.$watch('data.home', function (newValue, oldValue, scope) {
// logic for handling newvalue.baseValue
}, true);
$scope.$watch('data.contact', function (newValue, oldValue, scope) {
// logic for handling newvalue.baseValue
}, true);
... // Add more individual `watch` functions