3

I'm using FullCalendar plugin in a Angular project and trying to alert a value on select method of the plugin.

//Directive
app.directive('calendar', function(){
        return {
            restrict: 'A',
            scope: {
                select: '&'
            },
            link: function(scope, element, attrs) {
                element.fullCalendar({
                    selectable: true,

                    select: function(start, end, allDay) {
                        scope.select(start, end);

                        //alert(start);
                    }
                });
            }
        }
    }) 

app.controller('calendarCtrl', function() {
  this.onSelect = function(arg, arg2) {
    alert(arg + '' + arg2)
  }
});

HTML

<body ng-controller="calendarCtrl as clctrl">
    <div calendar select="clctrl.onSelect()"></div>
</body>

As you can see above in the directive, on selecting a day, I'm passing the onSelect() function which has the alert from controller. I'm trying to alert the first 2 return values(start and end) but I'm getting undefined values.

What is wrong in my code? I would appreciate if you could update plunker.

http://plnkr.co/edit/utxrmH8mYzo5jq9shcej?p=preview

1
  • Theres already an directive for the calendar with full implementation: github.com/angular-ui/ui-calendar. You are trying to reinvent the wheel Commented Jun 15, 2015 at 16:57

1 Answer 1

3
var app = angular.module('app', []);

//Controller
app.controller('calendarCtrl', function() {

  this.onSelect = function(arg, arg2) {

    alert(arg + ' ' + arg2)

  }

});

//Directive
app.directive('calendar', function(){
        return {
            restrict: 'A',
            scope: {
                select: '&'
            },
            link: function(scope, element, attrs) {

                //Generate the Calendar
                element.fullCalendar({

                    selectable: true,

                    //On Day Select
                    select: function(start, end, allDay) {
                        scope.select({start: start, end: end});

                        //alert(start);
                    }
                });
            }
        }
    })


<div calendar select="clctrl.onSelect(start, end)"></div>

updated plunker

Sign up to request clarification or add additional context in comments.

5 Comments

Nice job, can you explain why your solution works? I'm not familiar w/FullCalendar. Is putting the arguments inside the object required by Angular or FullCalendar?
You have to pass it as argument in directive and You have to give same exact key in html also. Then only it will work. If it is correct answer please upvote it and approve as answer.
This is nothing to do with FullCalender. This is a Angularjs feature.
Ok got it. The top part of this answer explains what Angular is doing when you use &. I've used & before, but never noticed this ... guess I wasn't passing args to the function.
Always welcome. Thanks @sunil for giving some good explanation what i did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.