I'm rendering dynamic content in my javascript template (EJS). After that, I receive through socket.io some part of the content for update. I was wondering if there was a proper way to do this without using some kind of $eval or $parse function.
Here's the code to render the template :
var data = [
"0":{
"id": "0",
"title": "Data 0",
"name": "Name of Data 0",
"description": "Desc of Data 0"
},
"1":{
"id": "1",
"title": "Data 1",
"name": "Name of Data 1",
"description": "Desc of Data 1"
},
"2":{
"id": "2",
"title": "Data 2",
"name": "Name of Data 2",
"description": "Desc of Data 2"
}
];
<% for(var i=0; i < data.length; i++) {%>
<div class="wrapper">
<h1><%=data[i].title%></h1>
<p><%=data[i].name%></p>
<p><%=data[i].description%></p>
</div>
<% } %>
Now, my socket.io app will send to this page one of the data with new content; for example
"1":{
"id": "1",
"title": "New Data 1",
"name": "New Name of Data 1",
"description": "New Desc of Data 1"
},
How could I update the correct DOM node in angular ? And if needed what do I need to change in the data sent by socket.io ?
I used to start this kind of code but this looks very ugly :
<script>
<% for(var i=0; i < data.length; i++) {%>
$scope.Data_<%=data[i].id%>_title = "<%=data[i].title%>";
$scope.Data_<%=data[i].id%>_name = "<%=data[i].name%>";
$scope.Data_<%=data[i].id%>_desc = "<%=data[i].description%>";
<% } %>
</script>
And the HTML :
<% for(var i=0; i < data.length; i++) {%>
<div class="wrapper">
<h1>{{Data_<%=data[i].id%>_title}}</h1>
<p>{{Data_<%=data[i].id%>_name}}</p>
<p>{{Data_<%=data[i].id%>_description}}</p>
</div>
<% } %>
ngRepeatbut another templating language. You are looking for an AngularJS example though right? It probably just would involve wrapping the code which receives the socket.io update in$apply