【问题标题】:Add events in full-calendar with Ionic 2使用 Ionic 2 在全日历中添加事件
【发布时间】:2017-07-26 19:50:49
【问题描述】:

我有一个 Ionic 2 应用程序,它使用的是 fullcalendar

我按照这些steps 在 Ionic 2 上使用它,效果很好。

但是现在,我需要从包含此自定义组件的页面组件动态地创建事件。

我有一个配置文件组件: - 在profile.ts 中,我正在从firebase 检索每个用户的日期。 - 在profile.html,我有这个选择器:<full-calendar></full-calendar>

所以,在个人资料页面中,我可以看到日历,并且我已经编辑了他们的样式。现在我想从此页面创建事件,但我不知道如何正确引用它。

我试过了:

import { FullCalendarComponent } from .....

@Component({
....
    providers: [FullCalendarComponent]
})

.
.
.
FullCalendarComponent.calendarOptions.events = myEvents;

但我看不到运行它的任何更改,控制台也不会向我抛出任何错误..

提前致谢!

编辑

在全日历组件(components/full-calendar)中:

full-calendar.html

<angular2-fullcalendar [options]="calendarOptions"></angular2-fullcalendar>

full-calendar.ts

import { Component } from '@angular/core';

@Component({
    selector: 'full-calendar',
    templateUrl: 'full-calendar.html'
})
export class FullCalendarComponent {

    calendarOptions: any = {  // Before it was type : Object
        //height: 'parent',
        locale: 'es',
        contentHeight: 'auto',
        fixedWeekCount : false,
        weekends: true,/**
        defaultDate: '2017-01-03',*/
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        defaultView: 'month', //basicWeek
        allDaySlot: false,
        minTime: '06:00:00',
        maxTime: '23:00:00',
        header: {
            left: 'prev',
            center: 'title', //'prev, title, next'
            right: 'next'
        },
        events: [
        {
            title: 'All Day Event',
            start: '2016-09-01'
        },
        {
            title: 'Long Event',
            start: '2016-09-07',
            end: '2016-09-10'
        }]
    }
}

在个人资料页面组件(页面/个人资料)中:

profile.html:(我有更多代码但只显示重要的)

<div class="calendar">
    <full-calendar [options]="calOptions" #mycal></full-calendar>
</div>

profile.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
import { FullCalendarComponent } from '../../components/full-calendar/full-calendar';
import * as $ from 'jquery';


@Component({
    selector: 'page-profile',
    templateUrl: 'profile.html',
    providers: [FullCalendarComponent]
})
export class ProfilePage {
@ViewChild('mycal', { read: ElementRef }) myCal: ElementRef;

calOptions: any = {
    locale: 'es',
    contentHeight: 'auto',
    fixedWeekCount : false,
    weekends: true,/**
    defaultDate: '2017-01-03',*/
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    defaultView: 'month', //basicWeek
    allDaySlot: false,
    minTime: '06:00:00',
    maxTime: '23:00:00',
    header: {
        left: 'prev',
        center: 'title', //'prev, title, next'
        right: 'next'
    },
    events: []
};

constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    public platform: Platform,
    public toastCtrl: ToastController,
    private loadingCtrl: LoadingController,
    public auth: FirebaseAuth
) {}

public setCalendarEvents(): void {

    let events: Array<any> = [];

    for (let entry of this.consecutiveDates) {
        let event = {
            allDay: true,
            title: ' ',
            start: entry
        };

        events.push(event);
    }

    this.calOptions.events = events;
    $(this.myCal.nativeElement).fullCalendar('addEventSource', events);
}

这让我很震惊:

Template parse errors:
Can't bind to 'options' since it isn't a known property of 'full-calendar'.
1. If 'full-calendar' is an Angular component and it has 'options' input, then verify that it is part of this module.
2. If 'full-calendar' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.

【问题讨论】:

  • 您是否 100% 确定没有错误?我想知道。你可以改用官方的离子组件?
  • 等等我有点困惑,他能显示 cal 但不能显示事件?
  • @Smit 抱歉,我忘了提到我可以在我创建的完整日历组件中看到在 calendarOptions 中声明为示例的事件:Events: [ { Title: 'All Day Event ', Start: '2016-09-01' }, { Title: 'Long Event', Start: '2016-09-07', End: '2016-09-10' } ] 但如果我执行 FullCalendarComponent.calendarOptions。 events = events,没有效果出现。
  • 现在我正在尝试使用以下方式引用完整日历: $("full-calendar").fullCalendar('addEventSource', {title:' ', start:'2017-03-06'}) ;但它不起作用。也许我没有正确引用它..
  • @IvanLencina 我已经添加了答案,您可以查看它。需要进行一些修改。

标签: jquery angular typescript fullcalendar ionic2


【解决方案1】:

.ts 有时不起作用。一些建议创建您自己的。

但这就是我所做的。在 .ts 中创建一个 elementRef。类变量

@ViewChild('mycal', { read: ElementRef }) myCal: ElementRef
calOptions: any = {}; //options

然后,当您有来自firebase 的事件时,请执行以下操作。其中slots 是您的events。以某种方式。

this.calOptions.events = slots
 $(this.myCal.nativeElement).fullCalendar('addEventSource', slots)

记住:slots 是对象数组而不是单个对象!

在html中:

<full-calendar [options]="calOptions "#mycal></full-calendar>

更多信息:https://gist.github.com/shah-smit/85aff341cd4a20494910ab2c17e82777/edit

【讨论】:

  • @ViewChild() ... 应该放在我的 profile.ts 中还是放在 full-calendar.ts 中?无论如何,我抛出了这个错误:模板解析错误:无法绑定到“选项”,因为它不是“完整日历”的已知属性。 1. 如果'full-calendar'是一个Angular组件并且它有'options'输入,那么验证它是这个模块的一部分。 2. 如果 'full-calendar' 是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的 '@NgModule.schemas' 以禁止显示此消息。
  • 以上代码应该在profle.ts中!您应该在这样做时导入 fullcalendar。
  • options 是完整日历的输入,如果 fullcalendar 不是已知属性,则您必须先解决该问题。
  • 是的,我正在这样做。我将在帖子中分享我的代码,我会告诉你看看它
  • 哦。现在我释放full-calendar 是你定义的选择器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2012-05-08
相关资源
最近更新 更多