hi,

I'm trying to work with models, save some data in Services used as models 
and then update the scope in the controller using binding.
>From my LoginController I call the login function of my Service. In short 
(I added a $watch to see if the loggedIn state changes):

myAppModule.controller('LoginController',['$scope', 'Service','LoginModel', 
function($scope, Service, LoginModel) {
     
    $scope.dataObject = {
        loggedIn: false
    };
    
//    $scope.dataObject.loggedIn = LoginModel.data.loggedIn;
    
    $scope.$watch(LoginModel.data.loggedIn, onLoggedInChange);

    function onLoggedInChange() {
        console.log('watch login: ', LoginModel.data.loggedIn);
    }
   
     Service.login(username, password);

..... }]);

The service called by the controller is shown below and  calls another 
(http) service which returns the data correctly. 
I inject the LoginModel service into this service:
myAppModule.service('Service', ['APIService','LoginModel', 
function(APIService, LoginModel) {
    
    console.log('Service loggedIn: ', LoginModel.data.loggedIn);
    
   //test:
    LoginModel.setLoggedIn('yes');//sets value in LoginModel bound to scope 
in LoginControlller, works.
    
    this.login = function(username,password)
    {

        //test
        LoginModel.setLoggedIn('no');//called from controller, sets value 
in LoginModel, but doesn't update LoginControlller
        
        console.log('service login:',  LoginModel.data.loggedIn);//outputs 
model value correctly
       
        var data = [
            {
command:"login",
username:username,
password:password
}];
         
        APIService.call(data).then(function(data) {
            console.log('Login return', LoginModel.data.loggedIn);//outputs 
model value correctly
            
            if(data.e || data.commands[0].e)
                LoginModel.setLoggedIn('no');
            else
                LoginModel.setLoggedIn('yes');
            
            console.log('Service loggedIn', 
LoginModel.data.loggedIn);//outputs model value correctly
        });
        
    }
   
}]);


And finally, the LoginModel:
angular.module('Plugin').service("LoginModel", [function() {
    
    this.data = {
      loggedIn:'init'
    }
    
    this.setLoggedIn = function(state) {
        this.data.loggedIn = state;
        console.log('LoginModel setLoggedIn', this.data.loggedIn);
    }
}]);

I've used strings 'init' 'yes' and 'no' just for testing this. 

The problem is that I'm trying to bind the model to the controller but 
binding fails when I call the login function in the service.
The watch is only triggered initially, when the value of 
LoginModel.data.loggedIn is 'init'
Am I talking to two models instead of one? 
Could it be a scope issue?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to