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.
Does it help if you define your resource like this?
app.factory('Session', function() {
var Session = {
data: { username: undefined }, // ensure object ref to attach to
updateSession: function(userName) {
Session.data.username = userName; // do NOT recreate the object above
}
};
return Session;
});
I think what may be happening (and I can't see all of your code) but I don't think username
is set when you take a reference in abcController
. By defining a shared reference in your object schema first that abcController can attach to, it will reflect the changes later when updateSession() is called.
Here's a working example using your service idea.
I found an alternate solution using emit
and broadcast
. Full code example in this jsfiddle
app.js
app.run(function($rootScope) {
/*
Receive emitted message and broadcast it.
Event names must be distinct or browser will blow up!
*/
$rootScope.$on('handleEmit', function(event, args) {
$rootScope.$broadcast('handleBroadcast', args);
});
});
controller.js
app.controller('userNameController',function ($scope, $log, Config)
{
$scope.submitUserName= function ()
{
// Session.updateSession($scope.userName);
$scope.$emit('handleEmit', {username: $scope.userName});
};
});
app.controller('abcController',function ($scope, $log, Config)
{
$scope.$on('handleBroadcast', function(event, args) {
$scope.userName = args.username;
});
//some other code
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.