AngularFire - 无法恢复用户在数据库中输入的时间 [英] AngularFire - impossible to recover the time entered by the user in database

查看:27
本文介绍了AngularFire - 无法恢复用户在数据库中输入的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DIV 中发布了我的背景图像的先例问题.它在当前日期成功运行.但是当我想根据用户在数据库中输入的时间使用模型starthourid"来调整背景图像时,它不起作用(它显示夜晚")!但是数据库没问题,我可以在 HTML 中显示nameid",用starthourid"显示startdateid".

这是我的代码的一部分:

HTML:

<div class="card" ><div class="sunrise item item-divider" ng-class="time"><h2 class="文本稳定">{{ event.nameid }} </h2><h3 class="text stable" id="header-date"><i class="icon ion-clock"></i>乐 {{ event.startdateid |日期:d MMM"}} à {{ event.starthourid |日期:时:分"}}</p>

<div class="row"><div class="col"><h3><i class="icon ion-ios-location"></i>location</h3></div><div class="col"><h3><i class="icon ion-ios-people"></i>人们</h3></div>

<button class="button button-stable" id="positive-btn-price" disabled>price</button><img class="imgProfilEventPositive" src="{{ event.userid.facebook.cachedUserProfile.picture.data.url }}"><div class="item item-divider" id="footerEventNotepositive"><p><i class="fa fa-star"></i>注意

</div></div>

控制器JS:

$scope.events = Event.all;$scope.event = {'nameid': ''};var h = new Date(event.starthourid).getHours(event.starthourid);如果 (h>=6 && h<11) {$scope.time="日出";} else if (h>=11 && h<18) {$scope.time="day";} else if (h>=18 && h<21) {$scope.time="日落";} 别的 {$scope.time="夜";}$scope.create = function() {$state.go('tabstep1');};$scope.close = function() {$state.go('tabdash');};

服务JS:

myApp.factory("Event", ["$firebaseArray", "$firebaseObject", function($firebaseArray, $firebaseObject) {var eventRef = new Firebase('https://myApp.firebaseio.com/Events/');var userRef = new Firebase('https://myApp.firebaseio.com/Users/');var events = $firebaseArray(eventRef);var 事件 = {所有:事件,得到:函数(事件){var eventInfo = $firebaseObject(eventRef);event.startdateid = new Date(event.startdateid);event.starthourid = 新日期(event.starthourid);返回 $firebaseObject(eventRef.child('Events').child(userid));},添加:函数(事件){var eventInfo = $firebaseArray(eventRef, userRef);event.userid = userRef.getAuth();event.startdateid = new Date(event.startdateid).toJSON();event.starthourid = new Date(event.starthourid).toJSON();返回 eventInfo.$add(event);}}返回事件;}]);

解决方案

@MikeChamberlain 给了你一些值得遵循的好点,但我认为这会帮助你走上正轨:

您说过您要从 Firebase 取回事件对象,并且您可以在 html 中重复它们,对吗?

如果这是正确的,那么我们接下来要做的就是操作 event.starthouid 的值,使其返回日出、日落等的值.

如果您的 event.starthourid 与 html 中的日期过滤器一起使用,那么我们知道我们应该有一些可以安全地传递给 Date 对象的内容.我们将创建自己的过滤器,将 event.starthourid 转换为我们可以与 ngClass 一起使用的对象:

myApp.filter('setImageClass', function(){返回函数(输入){var h = new Date(input).getHours();如果 (h>=6 && h<11) {返回{日出:真};} else if (h>=11 && h<18) {返回{天:真};} else if (h>=18 && h<21) {返回{日落:真};} 别的 {返回{晚上:真};}};});

过滤器是工厂,所以一定要在模块上注册它们.上面我使用了您已有的myApp"模块参考.

现在您应该能够在重复中执行以下操作(不要忘记删除您在此处添加的硬编码日出"类,然后只需更改 ng-class 以使用新过滤器):

Faites-moi savoir si vous avez besoin de plus d'information.;-)

I've posted a precedent issue with my Background image in DIV. It's work successfully with current date. But when I want to adapt Background Image with time entered by user in database with model "starthourid", it's don't work (It display "night") ! But the database is OK, I can display "nameid" in HTML and "startdateid" with "starthourid".

This is a part of my code :

HTML :

<div ng-repeat="event in events">

    <div class="card" >
        <div class="sunrise item item-divider" ng-class="time">
        <h2 class="text stable"> {{ event.nameid }} </h2>
        <h3 class="text stable" id="header-date">
        <i class="icon ion-clock"></i> Le {{ event.startdateid | date : "d MMM" }} à {{ event.starthourid | date : "HH:mm" }}</p>
      </div>
      <div class="row">
      <div class="col">
        <h3><i class="icon ion-ios-location"></i> location</h3></div>
      <div class="col"><h3><i class="icon ion-ios-people">
        </i> people</h3></div>
      </div>
      <button class="button button-stable" id="positive-btn-price" disabled>price</button>
      <img class="imgProfilEventPositive" src="{{ event.userid.facebook.cachedUserProfile.picture.data.url }}">
      <div class="item item-divider" id="footerEventNotepositive"><p> <i class="fa fa-star"></i> note </p> </div>
        </div>
      </div>

</div></div>

Controller JS :

$scope.events = Event.all;

  $scope.event = {'nameid': ''};

    var h = new Date(event.starthourid).getHours(event.starthourid);
    if (h>=6 && h<11) {
      $scope.time="sunrise";
    } else if (h>=11 && h<18) {
      $scope.time="day";
    } else if (h>=18 && h<21) {
      $scope.time="sunset";
    } else {
      $scope.time="night";
    }


    $scope.create = function() {
    $state.go('tabstep1');
};
$scope.close = function() { 
     $state.go('tabdash'); 
};

Services JS :

myApp.factory("Event", ["$firebaseArray", "$firebaseObject", function($firebaseArray, $firebaseObject) {
var eventRef = new Firebase('https://myApp.firebaseio.com/Events/');
var userRef = new Firebase('https://myApp.firebaseio.com/Users/');
var events = $firebaseArray(eventRef);

     var Event = {

         all: events,

         get: function (event){
          var eventInfo = $firebaseObject(eventRef);
          event.startdateid = new Date(event.startdateid);
          event.starthourid = new Date(event.starthourid);
                return $firebaseObject(eventRef.child('Events').child(userid));

              }
            ,
        add: function (event){
          var eventInfo = $firebaseArray(eventRef, userRef);
          event.userid = userRef.getAuth();
          event.startdateid = new Date(event.startdateid).toJSON();
          event.starthourid = new Date(event.starthourid).toJSON();
                return eventInfo.$add(event);
              }
       }
       return Event;

}]);

解决方案

@MikeChamberlain gave you some good points to follow, but I think this will help get you on track:

You said that you're getting back the events object from Firebase and you're able to repeat over them in your html, right?

If that's correct, then the next thing we need to do is manipulate the values of the event.starthouid so that it returns the values of sunrise, sunset etc.

If your event.starthourid works with the date filter in your html, then we know we should have something we can safely pass to the Date object. We're going to create our own filter to convert the event.starthourid to an object we can use with ngClass:

myApp.filter('setImageClass', function(){
  return function(input) {
    var h = new Date(input).getHours();
    if (h>=6 && h<11) {
      return {sunrise: true};
    } else if (h>=11 && h<18) {
      return {day: true};
    } else if (h>=18 && h<21) {
      return {sunset: true};
    } else {
      return {night: true};
    }
  };
});

Filters are factories, so make sure you register them on a module. Above I used the reference you already have for the 'myApp' module.

Now you should be able to do the following in your repeat (don't forget to remove the hard-coded "sunrise" class that you added here and then just change the ng-class to use the new filter):

<div ng-repeat="event in events" ng-class="{{event.starthourid | setImageClass}}">

Faites-moi savoir si vous avez besoin de plus d'information. ;-)

这篇关于AngularFire - 无法恢复用户在数据库中输入的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆