This is a line of code from Template 1 (the template displays a list of users). The controller for this is MainCtrl
.
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" ng-controller="MainCtrl">
<a data-toggle="modal" data-target="#configureUserModal" href="#" ng-click="getUserDetails(user.id)">{{user.name}}</a>
</div>
<modal-configure-user></modal-configure-user>
The relevant code in my MainCtrl
is:
angular.module('app')
.controller('MainCtrl', ['$scope', '$http', '$window', '$rootScope', '$routeParams', '$timeout', function(
$scope, $http, $window, $rootScope, $routeParams, $timeout) {
$scope.getUserDetails = function (userId) {
$http.get("/user/" + userId).success(function(data) {
$scope.userData=data[0];
console.log($scope.userData.name); //Line 1
});
};
}]);
Now Template 2 for individual users opens when the User Name in Template 1 is clicked. I just want to display the user name in Template 2, so I do:
<div class="modal fade" id="configureUserModal" tabindex="-1" role="dialog" ng-controller="MainCtrl">
{{userData.name}} //Line 2
</div>
The controller for Template 2 is also MainCtrl
. But the problem is Line 1
works (prints the user name to console), Line 2
doesn't (it just shows up as null). I can't seem to figure out what the problem is. Is it a timing issue? Is Template 2 loaded faster than the HTTP request?