【问题标题】:How to use the $scope out restangular function如何使用 $scope out restangular 函数
【发布时间】:2013-12-03 16:31:17
【问题描述】:
Restangular.all('events').getList().then(function(event) {
    $scope.events = event;
});

$scope.calConfig = {
        views: [
             {"type": "agenda", "label": "Agenda"}, 
             {"type": "agendaDay", "label": "Day"}, 
             {"type": "agendaWeek", "label": "Week"}, 
             {"type": "month", "label": "Month"},
        ],
        view: "agendaWeek",
        eventSources: [$scope.events],
        now: moment().toDate(),

            /*/Callback Methods */
        },

我正在尝试使用 restangular 从我的 MongoDB 获取数据,然后将这些数据用于全日历。问题是 $scope.events 在使用 restangular 块时未定义。

如何将它用于 $scope.calConfig?

【问题讨论】:

    标签: javascript mongodb angularjs fullcalendar restangular


    【解决方案1】:

    这行代码 $scope.calConfig = { ... } 在 Restangular 调用之前运行。

    Restangular 调用完成后,您需要设置配置。

    Restangular.all('events').getList().then(function(event) {
        $scope.events = event;
    
        $scope.calConfig = {
            views: [
                 {"type": "agenda", "label": "Agenda"}, 
                 {"type": "agendaDay", "label": "Day"}, 
                 {"type": "agendaWeek", "label": "Week"}, 
                 {"type": "month", "label": "Month"},
            ],
            view: "agendaWeek",
            eventSources: [$scope.events],
            now: moment().toDate(),
    
                /*/Callback Methods */
        };
    });
    

    这是因为 Restangular 代码是异步的。您基本上是在要求它执行某些操作,并且 then 在完成后调用一个函数。

    有关这方面的更多信息,请查看http://docs.angularjs.org/api/ng.$q


    更新:这会将您的配置代码移动到它自己的函数中,并在请求返回肯定响应时调用它。您可能应该处理否定的响应。

    Restangular.all('events').getList().then(function(event) {
        $scope.events = event;
        $scope.config();
    });
    
    $scope.config = function() {
        $scope.calConfig = {
            views: [
                 {"type": "agenda", "label": "Agenda"}, 
                 {"type": "agendaDay", "label": "Day"}, 
                 {"type": "agendaWeek", "label": "Week"}, 
                 {"type": "month", "label": "Month"},
            ],
            view: "agendaWeek",
            eventSources: [$scope.events],
            now: moment().toDate()
        };
    }
    

    【讨论】:

    • 这就是我的想法,但显然我不想将 $scope.calConfig 放入 then(),因为 $scope.calConfig 是一个巨大的块,我没有将它们全部发布.那么我们还有其他方法可以做到这一点吗?
    • @user1070111 你这样做,你可以在回调上调用一个方法。查看我的编辑。
    【解决方案2】:

    restangular 调用是异步的;您需要将$scope.calConfig 放入回调中,或者在执行回调时更新配置。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2023-01-11
      • 1970-01-01
      相关资源
      最近更新 更多