第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用AngularFire基于id在路徑之間連接數(shù)據(jù)

使用AngularFire基于id在路徑之間連接數(shù)據(jù)

慕萊塢森 2019-07-25 10:31:42
我目前正在使用firebase和angularJS(離子)開發(fā)應(yīng)用程序?;旧线@是一個汽車管理應(yīng)用程序,所以你有人與他人分享他們的汽車。我試圖將數(shù)據(jù)結(jié)構(gòu)盡可能平坦以提高效率。我的問題是如果沒有問題我可以顯示與登錄用戶共享的不同汽車的car_id列表,我找不到顯示與顯示年份和模型的用戶共享的汽車列表的方法。預(yù)先感謝您的幫助 !{"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貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個贊

users/路徑中,首先按索引存儲汽車列表,而不是存儲在數(shù)組中。所以你的結(jié)構(gòu)將是:

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

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

要使用AngularFire加入此項(xiàng),您可以使用多種方法。一個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);});

對于更復(fù)雜和更優(yōu)雅的方法,可以使用NormalizedCollection并將該ref傳遞給AngularFire數(shù)組:

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);});


查看完整回答
反對 回復(fù) 2019-07-25
  • 1 回答
  • 0 關(guān)注
  • 474 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號