0

I have a controller and want to pass a value of a scope to another.

app.controller("ChatController", function($scope, $firebaseArray, $timeout, $firebaseAuth, $window) {
  var ref = firebase.database().ref().child("messages");
  var auth = $firebaseAuth();

  $scope.messages = $firebaseArray(ref);

  $scope.sendMessage = function() {

   //var firebaseUser = $scope.firebaseUser;

    $scope.messages.$add({
      text: $scope.newMessageText,
      current_time: new Date().toJSON()
    });
    $scope.newMessageText = null;
  };

  $scope.login = function() {
      $scope.firebaseUser = null;
      $scope.error = null;

      auth.$signInWithPopup('google').then(function(firebaseUser) {
        $scope.firebaseUser = firebaseUser; //access this to the sendMessage scope
        console.log(firebaseUser.user);
      }).catch(function(error) {
        $scope.error = error;
      });

  };

});

Above me, I have sendMessage and login scope. I want to pass the value of $scope.firebaseUser to the login scope. Is that possible?

4
  • Still not got what are you exactly looking for after reviewed your code. Commented Nov 8, 2016 at 4:12
  • @Jigar7521 updated the code, you can see a new comment in the sendMessage scope. That's what I am trying to achieve. Commented Nov 8, 2016 at 4:14
  • 1
    ok now it's pretty understandable Commented Nov 8, 2016 at 4:17
  • Please check my answer Commented Nov 8, 2016 at 4:20

2 Answers 2

2

I think you mixed up Javascript lexical scoping and Angular scopes. In Angular, scope is at the controller level. This means everything within a controller has access to the controller's scope. In turn this means that you can just use the $scope.firebaseUser within your sendMessage function like so:

$scope.sendMessage = function() {

   var firebaseUser = $scope.firebaseUser;

    $scope.messages.$add({
      text: $scope.newMessageText,
      current_time: new Date().toJSON()
    });
    $scope.newMessageText = null;
  };

Have you tried this? Did you run into any issues?

Please see here for more details

Sign up to request clarification or add additional context in comments.

1 Comment

Oh. It worked. I'm actually thinking of passing to another controller lol
0

I have made some edits inside it, please experiment it and let me know, i am waiting. I have used $rootScope for this scenario which is present as a global variable in angular.

Note : I am writing this code by predict that your existing code is correct totally.

app.controller("ChatController", function($scope, $firebaseArray, $timeout, $firebaseAuth, $window, $rootScope) { var ref = firebase.database().ref().child("messages"); var auth = $firebaseAuth(); $rootScope.firebaseUser = null; $scope.messages = $firebaseArray(ref);

      $scope.sendMessage = function() {

       var firebaseUser = $rootScope.firebaseUser;


        $scope.messages.$add({
          text: $scope.newMessageText,
          current_time: new Date().toJSON()
        });
        $scope.newMessageText = null;
      };

      $scope.login = function() {
          $scope.firebaseUser = null;
          $scope.error = null;

          auth.$signInWithPopup('google').then(function(firebaseUser) {
            $scope.firebaseUser = firebaseUser; //access this to the sendMessage scope
          $rootScope.firebaseUser = firebaseUser.user;
            console.log(firebaseUser.user);
          }).catch(function(error) {
            $scope.error = error;
          });

      };

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.