add to firebase with angularfire $add

Asked

Viewed 184 times

1

My scripts:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/2.2.0/angularfire.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>

Hello I am beginner with firebase and angular, I have the following situation in my bank:

inserir a descrição da imagem aqui

obs. manually entered values.

Now I would like to leave dynamic with angular, and I am doing this way the controller:

   app.controller("CtrlList", function($scope, $firebaseArray){

    var ref = firebase.database().ref().child("users");

    $scope.addUser = function(){
      $scope.users.$add({
        text: $scope.newUserText
      });
    };

And my fluffy like that:

<form ng-submit="addUser()">
  <input ng-model="newUserText" />
  <button type="submit">Add User</button>
</form>

Being a beginner I’m a little lost and can not save in firebase, I believe I lack some concept.

1 answer

1


I record the resolution of my problem, so the controller was as follows:

app.controller("CtrlList", function($scope, $firebaseArray){

    var ref = firebase.database().ref().child("users");

    data = $scope.users = $firebaseArray(ref);

    $scope.addUser = function(){          
      writeUserData($scope.newUserId, $scope.newUserName);          
    };

    function writeUserData(userId, name) {
      firebase.database().ref('users/' + userId).set({
        name: name
      });
    }

  });

And the form thus:

<form ng-submit="addUser()">
  <input ng-model="newUserId" placeholder="User" />
  <input ng-model="newUserName" placeholder="Name" />
  <button type="submit">Add User</button>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.