I have two controllers that communicate with each other.
Controller 1:
$scope.$on("reloadUsers", function (event) {
$http.get("/users").success(function(data) {
$scope.users = data;
console.log($scope.users);
});
});
$http.get("/users").success(function(data) {
$scope.users = data;
});
Controller 2:
$scope.editUser = function(id){
$http({
url: "/users/"+ id,
method: "PUT",
data: {
"firstName":$scope.user.firstName,
"lastName": $scope.user.lastName,
}
});
$scope.$emit("reloadUsers");
}
My templates are as shown below (both modal dialogs):
Template 1:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" ng-controller="Controller 1">
<div ng-repeat="user in users>
<a data-toggle="modal" data-target="#configureUserModal" href="#" ng-click="getUserId(user.id)" data-id="user.id">{{user.first_name + ' ' +user.last_name}}</a>
</div>
</div>
Template 2:
<div ng-controller="Controller2" class="modal fade" id="configureUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<label for="">First Name:</label>
<input type="text" value="{{userData.first_name}}" ng-model="user.first_name" />
<label for="">Last Name:</label>
<input type="text" value="{{userData.last_name}}" ng-model="user.last_name" />
<button type="button" class="btn btn-default" ng-click="editUser(userData.id)" data-dismiss="modal">Save</button>
</div>
Essentially Template 1
has a list of users that I get from Controller 1's
scope. On clicking a user in Template 1
, I go to Template 2
which displays the user and has an option to edit (change first name and last name). Once I hit Save
, I make a HTTP PUT request, save the data, close the modal windows and call emit
.
The problem is: When I open Template 1
after this, I see the old first name and last name (not the updated ones). But when I click on the user to open Template 2
, the updated names are shown. How do I ensure the data is updated in Template 1
? Isn't angular supposed to update it when it sees a change in the $scope
variable?