2015-05-09 40 views
0

我綁定用戶的詳細信息,以像這樣在火力地堡一節顯示保存/保存的消息:如何在火力地堡/ AngularFire綁定對象時

var userDetails = firebase.child('users').child(userID); 
$firebaseObject(userDetails).$bindTo($scope, 'user'); 

我有一個數據綁定到一些HTML表單(例如ng-model=user.Name

我試圖弄清楚HTML項目如何改變它顯示一個帶有「保存」的消息,然後顯示一條消息說「保存」一旦Firebase更新完成。

回答

3

這裏是$save method爲了解救。這就是所謂的模型變化即ng-change

//html 
<input type="text" ng-model="userObj.name" ng-change="userObj.$save(user)" /> 


//js 
var userObj = $firebaseObject(ref); 

userObj.$save().then(function(ref) { 

    $scope.notifyUserSaved()//here you do notification 

}, function(error) { 
    console.log("Error:", error); 
}); 

UPD。使用rootScope:

//in controller 
userObj.$save().then(function(ref) { 

    //$scope.notifyUserSaved() 
    //here you do notification 
    $rootScope.$broadcast("saving", {"foo":"bar"}); 

}, function(error) { 
    console.log("Error:", error); 
}); 

//in other modules 
$rootScope.$on("saving", function(){ 
    //notify user globally here 
}) 
+0

有無論如何要做到這一點全球?恩。觀察是否有更新發生,以顯示保存消息。 – Jordash

+0

$ rootScope相當於AngularJS應用程序中的'全局'範圍。 https://docs.angularjs.org/api/ng/type/$rootScope.Scope。檢查更新的帖子 – shershen