Sergey Pavlov
Sergey Pavlov

Reputation: 147

Have a trouble with using $bindTo twice

I'm new to AngularJs and now I'm trying to work with Firebase.

This is my Firebase data looks like:

pavlovdog
|
 ---facebook: "facebook.pavlovdog.com"
|
 ---twitter: "@pavlovdoggy"

And my JavaScript code:

angular.module('starter.controllers', ['firebase'])

.controller('DashCtrl', ['$scope','$firebaseObject','$firebaseArray',
function($scope,$firebaseObject,$firebaseArray) {

    // Syncing to firebase
    var ref = new Firebase("https://#####.firebaseio.com/pavlovdog/");
    var syncObject = $firebaseArray(ref); 
    syncObject.$bindTo($scope, "contacts");
}])

.controller('NewListCtrl',['$scope', '$firebaseObject','$firebaseArray', 
    function($firebaseObject,$firebaseArray,$scope){
        var ref = new Firebase("https://#####.firebaseio.com/pavlovdog/");
        var syncObject = $firebaseArray(ref);
        syncObject.$bindTo($scope,"newUser");

}])

In DashCtrl everything is working great and I can use $scope.contacts. But in NewListCtrl simular code doesn't work. What's the problem?

I'm using NewListCtrl right after DashCtrl, maybe it matters

Upvotes: 2

Views: 240

Answers (1)

Sunil D.
Sunil D.

Reputation: 18193

On the surface the first problem is with the dependency injection syntax for NewListCtrl.

When you specify the list of dependencies, the order matters. So this line:

.controller('NewListCtrl',['$scope', '$firebaseObject','$firebaseArray',

Is stating that Angular should inject three items into your controller when creating it, and in that specific order.

Notice, however, that your function arguments are not in the same order in this controller:

function($firebaseObject,$firebaseArray,$scope)

This is sure to cause things to go haywire in your code. What you think is the $scope is really $firebaseArray, and so on.

Upvotes: 3

Related Questions