JS:
angular.module('ShareGoalsApp')
.controller('newGoalCtrl', function($scope, $ionicPopup, $firebaseArray) {
var goal_collection = new Firebase("https://sharegoals.firebaseio.com/sharegoals");
$scope.goals = $firebaseArray(goal_collection);
$scope.addNewGoal = function(goals) {
$scope.goals.$add(goals);
};
});
与上述控制器相关的HTML:
<ion-pane view-title="goal">
<ion-header-bar class="bar-positive">
<div class="buttons">
<a nav-transition="android" class="button button-icon icon ion-arrow-left-b" ng-click="" href="#/index"></a>
</div>
<h1 class="title">Add New Goal</h1>
</ion-header-bar>
<ion-content class="padding has-header has-footer" scroll="false" >
<div class="list">
<label class="item item-input">
<input type="text" placeholder="#Title" ng-model="goals.gTitle">
</label>
<label class="item item-input">
<span class="hashtag-title">#{{goals.gTitle}}</span>
</label>
<label class="item item-input">
<textarea placeholder="Goal" ng-model="goals.gDescription"></textarea>
</label>
</div>
</ion-content>
<ion-tabs class="tabs-icon-top tabs-color-active-positive">
<button class="button button-positive button-bar no-round-corner" ng-click="addNewGoal(goals)">Add Goal</button>
</ion-tabs>
</ion-pane>
firebase会发生什么:
[id]
name: "value1"
[id2]
0
name: "value1"
name2: "value2"
[id3]
0
name: "value1"
1
name2: "value2"
name3: "value3"
因为它添加了新数据......它在它之前获取数据然后将其添加到新数据中...它现在在整个firebase存储中都有冗余值...
答案 0 :(得分:1)
您的问题出在addNewGoal方法中。首先,您可以使用完整的目标
$scope.goals = $firebaseArray(goal_collection);
当您调用addNewGoal时,您将相同的数组提供给该函数,并在使用$scope.goals.$add(goals);
如果您只想添加一个新目标,我建议为该新目标使用单独的$ scope变量并将其传递给addNewGoal函数,这应该可以。