I'm having problems to set a variable from the directive and use it in the scope of my controller.
In my directive I have:
function full_calendar($http, $compile) {
return {
restrict: 'A',
link: function(scope, element, attr) {
$(document).on('click', '#calendar .fc-toolbar .fc-right .fc-button-group .fc-button', function(){
var b = $('#calendar').fullCalendar('getDate');
var calendar_date = b.format('L');
$http.post('/api/calendar', { date: calendar_date })
.success(function (data, status, headers, config) {
console.log(data);
// place the data in html
var item = data.items.item;
scope.calendarData = item; // THIS IS THE VARIABLE I WANT TO SET
//scope.$apply();
})
.error(function(data, status, headers, config) {
console.log('error');
})
;
});
}
};
}
angular.module('formApp')
.directive('fullCalendar', ['$http', '$compile', full_calendar]);
As you can see I want to set scope.calendarData
and use that in my controller. I tried to use scope.$apply();
but that gave me the following error: http://errors.angularjs.org/1.2.16/$rootScope/inprog?p0=%24digest .
In my controller I have:
angular.module('formApp', ['ngAnimate', 'ui.router', 'ui.calendar'])
.controller('formController', function($scope, $http, $compile, uiCalendarConfig) {
$scope.calendarData = [];
// ui calender
$scope.eventSources = [];
$scope.uiConfig = {
calendar:{
height: '100%',
editable: true,
header:{
right: 'prev,next'
},
dayClick: function(date, jsEvent, view, resourceObj)
{
console.log($scope.calendarData);
}
}
};
});
in the function of dayClick I would like to access the variable. The click function on button is already executed ... . What am I doing wrong?