在控制器(范围)中使用指令中的变量(在http post之后)

时间:2015-09-22 13:26:41

标签: angularjs angularjs-directive angularjs-scope angular-ui angularjs-controller

我遇到了从指令设置变量的问题,并在我的控制器范围内使用它。

在我的指示中,我有:

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]);

如您所见,我想设置scope.calendarData并在我的控制器中使用它。我尝试使用scope.$apply();,但这给了我以下错误:http://errors.angularjs.org/1.2.16/ $ rootScope / inprog?p0 =%24digest。

在我的控制器中我有:

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);
            }
        }
    };
});

在dayClick的功能中我想访问变量。按钮上的单击功能已经执行....我做错了什么?

0 个答案:

没有答案
相关问题