I have global variable $scope.posts in controller Angular JS:
I do increment this variable:
$scope.posts = $scope.posts + 1;
So my IDE editor underlines this code and tells:
Value assigned to primitive wil be lost
What is mean and how to fix?
In your app.js or main Angular module init the variable with this
.run(function ($rootScope) {
$rootScope.posts;
})
Then anywhere you want to increase this use the syntax
$scope.posts += 1;
It increments the value by one.
$rootScope is probably not what you want. You can share data across your app with a service or a factory. I've made a small Gist to show how this works, check it out here.
postsis a primitive (non-object) property of$scopein this instance, but being assigned to$scopedoes not make the property global. Your IDE is likely warning you that you are overwriting this property in another controller, but without more code, that can't be confirmed. See stackoverflow.com/questions/14049480/… for a more in depth analysis of this and potential solutions.