简体   繁体   English

在AngularJS中使用模板和过滤

[英]Using templating and filtering in AngularJS

I'm pretty new to Angular, and I've run into an issue. 我对Angular很陌生,但遇到了一个问题。 I'm hoping that there is an easy way to solve this problem. 我希望有一种简单的方法可以解决此问题。

Basically, I have a list of Appointments that I retrieve from the server. 基本上,我有一个从服务器检索的约会列表。

$scope.appointments = [{starts_at: "2015-1-1", name: "First"}, {starts_at: "2015-1-1", name: "Last"}, {starts_at: "2015-1-2", name: "Next Day"}];

Now I'd like to show these appointments in a list. 现在,我想在列表中显示这些约会。 I decided that I could use ng-repeat + filters for this, but now I'm stuck. 我决定可以为此使用ng-repeat +过滤器,但现在遇到了麻烦。

<ion-slide-box on-slide-changed="slideHasChanged($index)">
  <ion-slide>
    <div class='box'>
      <div class="list list-inset" ng-repeat="appt in appointments | filter:????>
        <div class="item" ng-click="openAppointmentModal(appt)">
          <span>Appt {{appt.name}}</span>
          <span>Starts at {{appt.starts_at}}</span>
        </div>
      </div>
    </div>
  </ion-slide>
</ion-slide-box>

I wasn't sure if I could use the built-in filterFilter to do this, so I ended up writing my own filter filterDateFilter , which takes in an array, a property name, and what date you want to filter for. 我不确定是否可以使用内置的filterFilter来执行此操作,所以最终我写了自己的过滤器filterDateFilter ,它接受一个数组,一个属性名称以及要过滤的日期。 I can then write something like ng-repeat='appt in appointments | filterDate:starts_at:curDate' 然后,我可以ng-repeat='appt in appointments | filterDate:starts_at:curDate'编写类似ng-repeat='appt in appointments | filterDate:starts_at:curDate' ng-repeat='appt in appointments | filterDate:starts_at:curDate' where curDate is defined by $scope.curDate = new Date() in the controller. ng-repeat='appt in appointments | filterDate:starts_at:curDate' ,其中curDate由控制器中的$scope.curDate = new Date()定义。

Phew. 唷。 That works, but only for filtering for whatever $scope.curDate is defined as. 那行得通,但仅适用于过滤定义为$ scope.curDate的任何内容。

Now, I want to the user to be able to swipe to the previous/next days, and show only the appointments for that day. 现在,我希望用户能够滑动到前一天/后一天,并仅显示该天的约会。 I assume I'd have to move the <ion-slide> to a template, but I'm not sure where to go from there. 我想我必须将<ion-slide>移到模板上,但是我不确定从那里去哪里。 What are my next steps? 我下一步要做什么?

Sorry for the long post and detailed explanation, but I didn't want to just throw up a question and show no previous effort. 很抱歉提供冗长的帖子和详细的说明,但我不想只提出一个问题并且不显示任何先前的努力。

I think that instead of ion-slide, you can use the ionic on-slide-left and on-slide-right directives to call a function you define in your controller which increments or decrements $scope.curDate by 1. This seems a simpler alternative to me than simulating infinite slides. 我认为可以使用ionic on-slide-lefton-slide-right指令而不是ion-slide来调用您在控制器中定义的函数,该函数将$scope.curDate递增或递减1。这似乎更简单对我而言,比模拟无限的幻灯片要好。

http://ionicframework.com/docs/api/directive/onSwipeLeft/ http://ionicframework.com/docs/api/directive/onSwipeLeft/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM