TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
michael neo
NA
10
1.3k
Angularjs allows data updates only for the first row
Jun 10 2018 3:49 PM
I have a code that displays three rows of information from json using angularjs.
Now I want if any values entered in the form inputs corresponding to that rows to replace the already existing values in the view for that rows.
Eg. In row 1 , I have value hello sub 1 for subcomment Variable. Now I want if I enter another values say New sub 1 in the form inputs on that, it will replace/updates the already existing values in the angular view for that rows.
The problem with the code above is that it keeps on replacing/updating only the subcomment values of the first row each time a new subcomment is entered. irrespective of the save button that was clicked
You can run the script and see the problem. Try to enter a value in a form input at row 2 or 3 and click on save. Instead of the updates to appear on view on the corresponding rows for subcomment at 2 or 3, it will keep appearing on the first row on the view
below is the entire partially working code which keeps updating only the first row no matter which submit button that was clicked
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"
></script>
<body ng-app=
"test"
ng-controller=
"testCtrl"
>
<div ng-repeat=
'post in posts'
>
<h3>Row {{ post.id }} </h3>
<b>Post Title:</b> {{ post.title }}
<div ng-repeat=
'comment in post.comment'
><br>
<b>sub comment</b>: {{comment.subcomment}} (<--New Subcomment appears)
<div>
<table>
<tr>
<tr>
<td>Enter subcomment</td>
<td><input type=
'text'
ng-model=
'subcomment'
placeholder=
"Enter Value For subcomment and Save."
></td>
</tr>
<tr>
<td> </td>
<td><input type=
'button'
id=
'but_save{{$index}}'
value=
'Save and Replace/Update'
ng-click=
'setResponse4(post.id,1,$index,subcomment,comment.id)'
></td>
<td><input type=
'button'
style=
"display:none"
ng-init=
'getComments1(comment.id,5)'
></td>
</tr>
</table>
</div>
</div>
<hr style=
"width:100%;font-size:10"
>
</div>
</body>
<script>
var
app = angular.module(
'test'
, []);
app.controller(
'testCtrl'
, [
'$scope'
,
'$http'
,
function
($scope, $http) {
//var app = angular.module('test',[]);
//app.controller('testCtrl',function($scope,$http){
$scope.posts = [
{
"id"
:
"1"
,
"title"
:
"my first title."
,
"comment"
: [
{
"subcomment"
:
"Hello sub 1"
}
]
},
{
"id"
:
"2"
,
"title"
:
"my second title."
,
"comment"
: [
{
"subcomment"
:
"Hello sub 2"
}
]
},
{
"id"
:
"3"
,
"title"
:
"my third title."
,
"comment"
: [
{
"subcomment"
:
"Hello sub 3"
}
]
}
];
$scope.comment=[];
//initialize an arrays of comments
$scope.setResponse4 =
function
(postid,type,index,subcomment,commentid){
angular.forEach($scope.posts[index].comment,
function
(value, key){
value.subcomment = subcomment;
//alert(value.subcomment);
});
//var subcomment=subcomment;
//$scope.posts[index].comment = subcomment;
}
}
]);
</script>
Reply
Answers (
0
)
How to start Angular.
How to handle big data using mat datatables in angular 5