亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用AngularFire基于id在路徑之間連接數據

使用AngularFire基于id在路徑之間連接數據

慕萊塢森 2019-07-25 10:31:42
我目前正在使用firebase和angularJS(離子)開發應用程序?;旧线@是一個汽車管理應用程序,所以你有人與他人分享他們的汽車。我試圖將數據結構盡可能平坦以提高效率。我的問題是如果沒有問題我可以顯示與登錄用戶共享的不同汽車的car_id列表,我找不到顯示與顯示年份和模型的用戶共享的汽車列表的方法。預先感謝您的幫助 !{"rules": {    "users": {        ".write": true,        "$uid": {            ".read": "auth != null && auth.uid == $uid"        },        "cars": {          "car_id":true,          "role":true // Owner, borower...        }    },    "cars": {      "car_id":true,      "model":true,      "year":true    }}}carapp.controller("carsController", function($scope, $firebaseObject, $ionicPopup, $ionicHistory) {$ionicHistory.clearHistory();$scope.list = function() {  frbAuth = frb.getAuth();  if(frbAuth) {    var userObject = $firebaseObject(frb.child("users/" + frbAuth.uid));    userObject.$bindTo($scope, "user");    $scope.cars = frb.child("cars");}}$scope.createCar = function() {  $ionicPopup.prompt({    model: 'Create a new car',    inputType: 'text'  })  .then(function(result) {    if(result !== "") {      var newCar = $scope.cars.push({        model: result      })      var newCarId = newCar.key();      $scope.user.cars.push({car_id: newCarId, role: "owner" });    } else {        console.log("Action not completed");    }});}});    <div class="list">     <a ng-repeat="car in user.cars" >         <h2>{{car.car_id}}</h2> ----> works fine !<h2>{{car.model}}</h2> ----> How to get this working ?         <h2>{{car.year}}</h2> ----> How to get this working ?     </a></div>
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

users/路徑中,首先按索引存儲汽車列表,而不是存儲在數組中。所以你的結構將是:

{
   "users": {
      "kato": {
         "cars": {
            "DeLorean": true
         }
      }
   },

   "cars": {
      "DeLorean": {
          model: "DeLorean",
          year: "1975"
      }
   }}

要使用AngularFire加入此項,您可以使用多種方法。一個AngularFire專用解決方案可能看起來像這樣,利用$ extend

app.factory('CarsByUser', function($firebaseArray) {
   return $firebaseArray.$extend({
     $$added: function(snap) {
        return new Car(snap);
     },

     $$updated: function(snap) {
        // nothing to do here; the value of the index is not used
     },

     $$removed: function(snap) {
        this.$getRecord(snap.key()).destroy();
     },

     // these could be implemented in a manner consistent with the
     // use case and above code, for simplicity, they are disabled here
     $add: readOnly,
     $save: readOnly   });

  var carsRef = new Firebase(...).child('cars');
  function Car(snap) {
     // create a reference to the data for a specific car
     this.$id = snap.key();
     this.ref = carsRef.child(this.$id);
     // listen for changes to the data
     this.ref.on('value', this.updated, this);
  }

  Car.prototype.updated = function(snap) {
     this.model = data.model;
     this.year = data.year;
  }

  Car.prototype.destroy = function() {
    this.ref.off('value', this.meta, this);
  };

  function readOnly() { throw new Error('This is a read only list'); }});app.controller('...', function($scope, CarsByUser, authData) {
   // authenticate first, preferably with resolve
   var ref = new Firebase(...).child(authData.uid);
   $scope.cars = CarsByUser($scope);});

對于更復雜和更優雅的方法,可以使用NormalizedCollection并將該ref傳遞給AngularFire數組:

app.controller('...', function($scope, $firebaseArray) {
  var ref = new Firebase(...);
  var nc = new Firebase.util.NormalizedCollection(
     ref.child('users/' + authData.uid),
     ref.child('cars')
  )
  .select('cars.model', 'cars.year')
  .ref();

  $scope.cars = $firebaseArray(nc);});


查看完整回答
反對 回復 2019-07-25
  • 1 回答
  • 0 關注
  • 477 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號