0

I am new to Javascript and Ionic and I would like to know how to display the name of my user in the app itself.

  1. I have a connection to Firebase that works fine
  2. Here is the code of js file :

    var userId = '-KcntxrTC1eZjrkyycn4';
    return firebase.database().ref('/accounts/' + userId).once('value').then(function(snapshot) {
        var displayName = snapshot.val().name;
        console.log(displayName);
        // ...
    });
    
  3. My html file is controlled by the controller where this code is.

  4. The console log renders "Sebastien" so it works

  5. When I write {{displayName}} in my html document, nothing shows up.

What am I missing ?

EDIT - ADD CODE

Here is the code of my controller :

.controller('accountController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {

  var userId = '-KcntxrTC1eZjrkyycn4';
  return firebase.database().ref('/accounts/' + userId).once('value').then(function(snapshot) {
  var displayName = snapshot.val().name;
  $scope.displayName = snapshot.val().name;
  console.log(displayName);
  // ...


  });
}])

Here is the code of my HTML document :

<!-- *profile-name / name profile -->
                <h3 class="profile-name">{{diplayName}}</h3>

The thing is that I get the name to show up in the console as soon as I log in but when I visit the page that is controlled by the actual controller, it doesn't pick it up. Like it didn't understand that it has to take the data from Firebase.

1
  • 1
    You have to place the properties you want to use in your views on your $scope object: $scope.displayName = snapshot.val().name; Commented Feb 13, 2017 at 14:05

2 Answers 2

2

whatever you want to show in view, you need to add in $scope angular service

JavaScript

$scope.data = {
  fName: "Sanjay",
  lName: "Nishad"
}

HTML

<p> {{data.fName}} </p>
<p> {{data.lName}} </p>

In your case: You don't need to declare var displayName = snapshot.val().name; just add in $scope

$scope.displayName = snapshot.val().name;

or

$scope.displayData = snapshot.val();
Sign up to request clarification or add additional context in comments.

5 Comments

Hello thank you for your time but it is still not working, it writes the value in the console but still doesn't show up anything...
What is showing? And which element are you using? May be that element clear by some other JS code or spell mistake
I edited my initial code, and nothing is showing at all, not even an error in the console, nothing. It just leaves a blank space instead of the name.
try once $scope.$apply() after assigning the value
Thank you very much, indeed, it worked like a charm !
1

I think this is because angular does not pick up the database read because it is happening outside of its $digest cycle. If you wrap your code in an $apply, it will probably work:

firebase.database()
    .ref('/accounts/' + userId)
    .once('value')
    .then(function(snapshot) {
        var displayName = snapshot.val().name;
        console.log(displayName);

        $scope.$apply(function() {
            $scope.displayName = displayName;
        });
    // ...
});

Note: Remove the return statement. A controller should not return anything.

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.