I've been struggling with this for hours and could really use some help. I have a .json file containing 100 products structured like this:
[{
"ean": "3613132010420",
"brand": "NewBrand",
"desc": "A description",
"feature1": "",
"feature2": "",
"feature3": "",
"feature4": "",
"feature5": "",
"img": "",
"metric": {
"gender": "female"
},
"score":""
},
{
"ean": "3613132010420",
"brand": "NewBrand",
"desc": "A description",
"feature1": "",
"feature2": "",
"feature3": "",
"feature4": "",
"feature5": "",
"img": "",
"metric": {
"gender": "female"
},
"score":""
}]
After reading the JSON using $http and populating $scope.products, everything displays fine in a list. Now, I want to filter the products and update the score variable (after swiping on an option slider).
The view should then reflect these changes due to Angular's data-binding.
But I'm having trouble changing this variable within the $scope. Here's what I've tried so far without success:
$('.find-style-slider .slick-list').bind('touchstart click', function(){
angular.forEach($scope.products, function(value, key) {
$scope.products[key].score = '25'; //nothing happens
var obj = { score: '25' };
$scope.products[key].push(obj); //Uncaught TypeError: undefined is not a function
$scope.products.splice(key, 0, obj); //no error but $scope variable does not change
$scope.products[key].unshift(obj); //Uncaught TypeError: undefined is not a function
});
});
Do I need to call $apply() or make any updates elsewhere? Any help or hints would be greatly appreciated...
Edit: I think there might be an issue with how the $scope works.... I populate the $scope.products with a service:
productService.initDb().then(function(products) {
$scope.products = products;
});
If I place this
var obj = { score: '25' };
$scope.products.splice(0, 0, obj);
INSIDE the initDb function, the first element gets updated! But it doesn't work outside of that scope.
So now the question is: WHY? And how can I access the $scope.products from outside the service function?
I thought the $scope was consistent throughout the controller... confused