JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app="demo">
<div ng-controller="dem">
{{u.name}}<hr>
{{user.name}}
</div>
</div>
JavaScript
angular.module('meService',[])
.factory('me',function($timeout){
var obj={}
obj.user={name:'hello'}
$timeout(function(){
newUser={name:'israel'}
cloneInto(obj.user,newUser)
},1000)
return obj
})
var app=angular.module('demo',['meService'])
app.controller('dem',function($scope,me){
$scope.user=me.user;
})
function cloneInto(a,b){
var i
for (i in a){
if(typeof a[i]=='object'){
}
else
delete a[i]
}
for (i in b){
if (typeof b[i]=='object'){
if(!a[i]) a[i]={}
cloneInto(a[i],b[i])
}
else{
a[i]=b[i]
}
}
}
// Asserts:
console.clear()
var a,b
a={onlyInA:true,c:1,ob:{bef:1}}
b={onlyInB:true,c:2,ob:{o1:1}}
console.log('1.',a,b)
cloneInto(a,b)
console.log('-.',a,b)
console.assert(!a.onlyInA,'!a.onlyInA')
console.assert(a.onlyInB,'a.onlyInB')
console.assert(a.c==2,'a.c==2')
console.assert(a.ob,'a.ob')
console.assert(!a.ob.bef,'!a.ob.bef')