UPDATE: angular.extend
and Object.assign
both work fine, but which one is the better choice?
I am facing an issue where adding more values to an angularjs service variable overwrites the previous value. Here is a sample code snippet:
var testModule = angular.module('testmodule', [])
.controller('abc', function ($scope, mser)
{
mser.aa = {a:5, b:2};
mser.aa = {c:5, d:2};
$scope.inps = mser.aa.a
})
.service('mser', function() {
var aa = {}
return aa
});
After executing mser.aa = {c:5, d:2};
, the value of mser.aa.a
becomes undefined
.
What can be done to prevent this and insert all values successfully?