加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs-scope – AngularFire有时不会更新Firebase

发布时间:2020-12-17 18:07:34 所属栏目:安全 来源:网络整理
导读:下面的控制器使用表格和表格提供视图. 该表的相关部分是: tr ng-repeat =“obj in tab.col | filter:filterObj”ng-click =“select(obj)”ng-class =“isSelected(obj)” 如果单击一行,表单将显示obj的详细信息(其模型为tab.obj),您可以对其进行编辑.当用
下面的控制器使用表格和表格提供视图.
该表的相关部分是:

< tr ng-repeat =“obj in tab.col | filter:filterObj”ng-click =“select(obj)”ng-class =“isSelected(obj)”>

如果单击一行,表单将显示obj的详细信息(其模型为tab.obj),您可以对其进行编辑.当用户修改表单字段中的obj时,表会适当地获取更新,但firebase不会获得任何更新,而是添加新对象(这是通过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
      }
    });
  };
});
  }]);

});

我怎样才能做出类似这样的工作?应该通过集合转向手动同步?使用console.log进行debuging显示对象obj具有代码的所有阶段(它具有原型,哈希码等)应该具有的内容,只是firebase没有得到更新.

更新:

我有一个有效的解决方案.似乎这是一个绑定问题,或类似的东西,但我不知道发生了什么. tabs [i] .col = $scope.links.i的赋值看起来就像罪魁祸首(在这种情况下i =’open’: – ?.这可能是我对angular的用法.

controller scope ------

    // get the firebase data and store it in 
    var promise = angularFire(url,{})

    // 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)">

解决方法

我有一个有效的解决方案.似乎这是我身边的一个具有约束力的问题. tabs [i] .col = $scope.links.i的赋值看起来就像罪魁祸首(在这种情况下i =’open’: – ?.这可能是我对angular的用法.

该解决方案不是使用tabs [i] .col,而是使用$scope.links,即使用$scope变量而不是对$scope变量的引用.

controller scope ------

    // get the firebase data and store it in 
    var promise = angularFire(url,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)">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读