使用AngularFire基于id在路径之间连接数据

我目前正在使用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>


慕莱坞森
浏览 448回答 1
1回答

偶然的你

在users/路径中,首先按索引存储汽车列表,而不是存储在数组中。所以你的结构将是:{ &nbsp;&nbsp;&nbsp;"users":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"kato":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"cars":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"DeLorean":&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;"cars":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"DeLorean":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model:&nbsp;"DeLorean", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year:&nbsp;"1975" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;}}要使用AngularFire加入此项,您可以使用多种方法。一个AngularFire专用解决方案可能看起来像这样,利用$ extend:app.factory('CarsByUser',&nbsp;function($firebaseArray)&nbsp;{ &nbsp;&nbsp;&nbsp;return&nbsp;$firebaseArray.$extend({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$$added:&nbsp;function(snap)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Car(snap); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$$updated:&nbsp;function(snap)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;nothing&nbsp;to&nbsp;do&nbsp;here;&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;index&nbsp;is&nbsp;not&nbsp;used &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$$removed:&nbsp;function(snap)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.$getRecord(snap.key()).destroy(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;these&nbsp;could&nbsp;be&nbsp;implemented&nbsp;in&nbsp;a&nbsp;manner&nbsp;consistent&nbsp;with&nbsp;the &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;use&nbsp;case&nbsp;and&nbsp;above&nbsp;code,&nbsp;for&nbsp;simplicity,&nbsp;they&nbsp;are&nbsp;disabled&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$add:&nbsp;readOnly, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$save:&nbsp;readOnly&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;var&nbsp;carsRef&nbsp;=&nbsp;new&nbsp;Firebase(...).child('cars'); &nbsp;&nbsp;function&nbsp;Car(snap)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;create&nbsp;a&nbsp;reference&nbsp;to&nbsp;the&nbsp;data&nbsp;for&nbsp;a&nbsp;specific&nbsp;car &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.$id&nbsp;=&nbsp;snap.key(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.ref&nbsp;=&nbsp;carsRef.child(this.$id); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;listen&nbsp;for&nbsp;changes&nbsp;to&nbsp;the&nbsp;data &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.ref.on('value',&nbsp;this.updated,&nbsp;this); &nbsp;&nbsp;} &nbsp;&nbsp;Car.prototype.updated&nbsp;=&nbsp;function(snap)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.model&nbsp;=&nbsp;data.model; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.year&nbsp;=&nbsp;data.year; &nbsp;&nbsp;} &nbsp;&nbsp;Car.prototype.destroy&nbsp;=&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;this.ref.off('value',&nbsp;this.meta,&nbsp;this); &nbsp;&nbsp;}; &nbsp;&nbsp;function&nbsp;readOnly()&nbsp;{&nbsp;throw&nbsp;new&nbsp;Error('This&nbsp;is&nbsp;a&nbsp;read&nbsp;only&nbsp;list');&nbsp;}});app.controller('...',&nbsp;function($scope,&nbsp;CarsByUser,&nbsp;authData)&nbsp;{ &nbsp;&nbsp;&nbsp;//&nbsp;authenticate&nbsp;first,&nbsp;preferably&nbsp;with&nbsp;resolve &nbsp;&nbsp;&nbsp;var&nbsp;ref&nbsp;=&nbsp;new&nbsp;Firebase(...).child(authData.uid); &nbsp;&nbsp;&nbsp;$scope.cars&nbsp;=&nbsp;CarsByUser($scope);});对于更复杂和更优雅的方法,可以使用NormalizedCollection并将该ref传递给AngularFire数组:app.controller('...',&nbsp;function($scope,&nbsp;$firebaseArray)&nbsp;{ &nbsp;&nbsp;var&nbsp;ref&nbsp;=&nbsp;new&nbsp;Firebase(...); &nbsp;&nbsp;var&nbsp;nc&nbsp;=&nbsp;new&nbsp;Firebase.util.NormalizedCollection( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.child('users/'&nbsp;+&nbsp;authData.uid), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.child('cars') &nbsp;&nbsp;) &nbsp;&nbsp;.select('cars.model',&nbsp;'cars.year') &nbsp;&nbsp;.ref(); &nbsp;&nbsp;$scope.cars&nbsp;=&nbsp;$firebaseArray(nc);});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS