Bộ điều khiển dưới đây cung cấp chế độ xem với bảng và biểu mẫu. Phần liên quan của bảng là:AngularFire không cập nhật Firebase đôi khi
<tr ng-repeat="obj in tab.col | filter:filterObj" ng-click="select(obj)" ng-class="isSelected(obj)">
Nếu một hàng được nhấp, hình thức hiển thị các chi tiết của obj (mô hình của nó là tab.obj), và bạn có thể chỉnh sửa nó. Bảng được cập nhật một cách thích hợp khi người dùng sửa đổi một obj trong các trường biểu mẫu, nhưng firebase không nhận được bất kỳ bản cập nhật nào nhưng bổ sung các đối tượng mới (điều này được thực hiện với array.push (obj)).
define(['app'], function(app) {
app.controller('LinksCtrl',['$scope', '$filter', '$http','angularFire','angularFireAuth', function($scope,$filter,$http, angularFire, angularFireAuth) {
$scope.links = {};
$scope.tabs = [];
var url = "https://<url>.firebaseio.com/links";
angularFireAuth.initialize(url, {scope: $scope, name: "user", path: "/"});
var promise = angularFire(url, $scope, 'links', {})
$scope.select = function (item) {
for(i in $scope.tabs) {
var tab = $scope.tabs[i];
if (tab.active) {
tab.obj = item;
if (tab.obj.selected) {
tab.obj.selected = ! tab.obj.selected;
} else {
tab.obj.selected = true;
// $scope.$apply();
}
}
}
};
$scope.isSelected = function(obj) {
if (obj.selected && obj.selected === true) {
return "active";
}
return "";
}
promise.then(function() {
$scope.tabs.push({
title: 'links',
disabled: false,
active: false,
col: $scope.links.open, //this is an array of objects
obj: {}
});
$scope.tabs[0].active = true;
for(i in $scope.tabs) {
var tab = $scope.tabs[i];
tab.actions = app.actions();
// app.actions returns an array with more objects like the following doing CRUD and other basic stuff
tab.actions.push({
name: 'Delete link',
icon: 'icon-minus',
cond: function(tab) { if ('selected' in tab.obj) { return tab.obj.selected}; return false; },
func: function(tab) {
// splice tab.col or whatever modification
}
});
};
});
}]);
});
Làm cách nào để tôi thực hiện công việc này? nên chuyển sang đồng bộ thủ công qua bộ sưu tập? gỡ lỗi với console.log cho thấy obj đối tượng có những gì nó được cho là có trong tất cả các giai đoạn của mã (nó có nguyên mẫu, hashcode, vv), nó chỉ là firebase không nhận được các bản cập nhật.
Cập nhật:
tôi có một giải pháp mà làm việc. Dường như nó là một vấn đề ràng buộc, hoặc một cái gì đó như thế, nhưng tôi không chắc chắn những gì đang xảy ra. Việc chuyển nhượng tab [i] .col = $ scope.links.i trông giống như thủ phạm (trong trường hợp này i = 'mở': -. ?. Nó có thể là việc sử dụng của tôi về góc
controller scope ------
// get the firebase data and store it in
var promise = angularFire(url, $scope, 'links', {})
// build the tabs structure, where tabs[i].col = $scope.links.i
// build actions for each tab
// create an empty tabs[i].obj to use in the form model
ng-repeat scope -------
// iterate over the tab structure
<tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active" disabled="tab.disabled">
ng-repeat scope --------
// Iterate over the elements, so we can select one, and edit it, updating firebase
// If we iterate over tab.col, firebase does not get the updates, but If we iterate over
// the original $scope.links variable, it works as expected
<tr ng-repeat="obj in links[tab.title] | filter:filterObj | orderBy: 'date':true" ng-click="select(obj)" ng-class="isSelected(obj)">
Điều này có vẻ là một vấn đề ràng buộc cho các phạm vi khiêu vũ của hai lồng nhau ng-lặp lại trong xem. Nhưng tất cả các yếu tố là đối tượng, và nên làm việc: -? [link] (https://github.com/angular/angular.js/wiki/Understanding-Scopes) –
Người thích tìm kiếm giải pháp! Bạn có thể sao chép giải pháp của mình vào câu trả lời bên dưới không? Nó làm cho nó dễ dàng hơn cho người tìm kiếm trong tương lai để tìm thấy nó :) – mimming