I tried to pass data between controller and I made a service to do that. But somehow when I tried to get the data, it doesn't work. I have no idea where my mistake is. Here is the code:
This is the AngularJS file:
var app = angular.module('Application', []);
app.controller('NotificationListController', function($scope,$http, NotificationService, Data) {
var notificationList = NotificationService.getNotificationList();
notificationList.then(function(response){
console.log(response);
$scope.notificationData = response.data;
return response;
});
$scope.setEditedNotification = function(notification){
$scope.editedNotification = Data.setNotificationValue(notification);
}
});
app.controller('NotificationEditController', function($scope, Data, TemplateService){
$scope.editedNotification=Data.getNotificationValue();
});
app.service('Data', function(){
var NotificationData;
return{
getNotificationValue: function(){
return NotificationData;
},
setNotificationValue: function(notificationData){
NotificationData = notificationData;
}
}
});
This is my index.html
<tr ng-repeat="notification in notificationData">
<td>{{notification.name}}</td>
<td>{{notification.sender}}</td>
<td>{{notification.subject}}</td>
<td>{{notification.template.name}}</td>
<td><a class="btn btn-default" href="editNotification.html" role="button" ng-click="setEditedNotification(notification)">Edit</a></td>
<td><a class="btn btn-default" href="#" role="button" ng-click="deleteNotification(notification.id, $index)">Delete</a></td>
</tr>
edit.html:
<div class="form-group col-md-offset-1">
<label for="inputNotifName" class="col-sm-1 control-label">Name</label>
<div class="col-md-6">
<input ng-model="editedNotification.name" type="text" class="form-control" id="inputNotifName" placeholder="Notification name">
</div>
</div><br><br>
<div class="form-group col-md-offset-1">
<label for="inputNotifSender" class="col-sm-1 control-label">Sender</label>
<div class="col-md-6">
<input ng-model="editedNotification.sender" type="email" class="form-control" id="inputNotifSender" placeholder="Sender">
</div>
</div><br>
<div class="form-group col-md-offset-1">
<label for="inputNotifSubject" class="col-sm-1 control-label">Subject</label>
<div class="col-md-6">
<input ng-model="editedNotification.subject" type="text" class="form-control" id="inputNotifSubject" placeholder="Subject">
</div>
</div>
I debugged it and I found out that the value of $scope.editedNotification is not being updated when I called Dara.setNotificationValue(notification);. And also when I tried to get var NotificationData after I set it, the value still null. I guess there is something wrong at the way I call the service.
$scope.editedNotification = Data.setNotificationValue(notification);
? setNotificationValue() doesn't return anything so this is likely setting$scope.editedNotification
to null. Without seeing how you've setng-controller
it's possible that the shared scope is causing this issue to propagate to the other controller.