I am trying to build a simple username login system. This is just to record username which I can use in my various controllers.
But, for some reason, I am unable to get $scope.userName value in abcController
The essence of this problem is sharing data across controllers.
controller.js
app.controller('userNameController',function ($scope, $log, Config, Session)
{
Session.updateSession($scope.userName);
});
app.controller('abcController',function ($scope, $log, Config, Session)
{
$scope.userName=Session.data.username;
//some other code
});
resource.js
app.factory('Session', function() {
var Session = {
data: {},
updateSession: function(userName) {
Session.data = { "username": userName }
}
};
return Session;
});
As soon as a user clicks on LogIn button a twitter-bootstrap modal(popup) will appear and ask user to enter their username.
index.html
<div ng-controller="userNameController">
<button class="btn" id="enterUsernameBtn" href="#userNameModal" role="button" class="btn" data-toggle="modal" title="Enter Username">Login</button>
</div>
<!-- UserName Modal -->
<div id="userNameModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="userNameModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="userNameModalLabel">Enter your username</h3>
</div>
<div class="modal-body">
<p><b>UserId</b></p>
<div class="input-append">
<input class="pull-left" id="userIdTextBox" type="text" style="left:8px; width:160px" ng-controller="userNameController" ng-model="userName">
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Submit</button>
</div>
</div>
Please feel free to suggest, if there is better way to do what I am trying to do above.
app.run(function(Session) {}); //bootstrap session;in app.js which is referenced before any other .js files. That didn't help. Haven't I already attached the Session resource to abcController? Please help, angularJS noob here.