I really need some help here… I´m trying for hours now and can´t get it to work…
I have a .json file with 100 products 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":""
}]
I read the json with $http and put everything in $scope.products. The data is shown in a list, everything is fine. Now I want to filter the products and alter the score variable. The view should then also be updated due to the angular data-binding.
How can I change this variable in the $scope?
This is what I tried and nothing works:
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 update something or $apply()?
I would be thankful for any help/hint…
Edit: I think the $scope is not working like I thought…
I fill the $scope.products with a service:
productService.initDb().then(function(products) {
$scope.products = products;
});
When I put this
var obj = { score: '25' };
$scope.products.splice(0, 0, obj);
INSIDE the initDb function then the first elements gets updated!
But not outside… But why?
I thought the $scope is the same for the whole controller… confused