【问题标题】:Use an AngularJS Service in place of the url in KendoUI Scheduler使用 AngularJS 服务代替 KendoUI 调度程序中的 url
【发布时间】:2019-04-24 11:49:51
【问题描述】:

我一整天都在试图弄清楚在使用 AngularJS 的 kendoui 调度程序适配器时事件没有成功加载,我发现原因是服务调用获取数据;用它正确工作的 URL 替换它,但对我来说似乎很难看。有什么方法可以让我使用它并让它正常工作吗?

这是我使用的代码

angular.module('xxx.controllers').controller('CalendarioController', [
'$scope', '$rootScope', '$mdDialog', '$mdToast', 'CalendarioService',
function ($scope, $rootScope, $mdDialog, $mdToast, CalendarioService) {
    var self = this;
    self.scope = $scope;
    self.service = CalendarioService;
    self.scope.loading = false;

    self.scope.nuovoEventoCalendario = function (ev) {

        $mdDialog.show({
            controller: 'NuovoEventoCalendarioController',
            templateUrl: '/amministratori/calendario/form',
            parent: angular.element(document.body),
            targetEvent: ev,
            locals: {
                // nada
            }
        }).then(function (response) {
            if (response) {
                //caricaListaEtichette();
                // Qui apro il popup con la preview del barcode
                //  self.scope.anteprimaDiStampa(response, ev);
            }
        }, function () {

        });
    };

    self.scope.schedulerOptions = {
        date: new Date(),
        views: [
            "day",
            //"workWeek",
            "week",
            { type: "month", selected: true },

        ],
        //editable: {
        //    template: $("#customEditorTemplate").html(),
        dataSource: {
            batch: true,
            transport: {
                read: {
                    type: "GET",
                    datatype: "json",
                    url: "https://localhost:44301/api/calendario/elenco", //This works
                    contentType: "application/json; charset=utf-8"
                },
                //read: {
                //    url: CalendarioService.getElencoEventiCalendario(), //This not
                //    dataType: "json"
                //},

奇怪的是服务的方法定义如下

angular.module('xxx.services').factory('CalendarioService', [
'$http', function ($http) {
    var baseUrl = "api/calendario";

    return {
        getElencoEventiCalendario: function () {
    return $http.get(baseUrl + "/elenco");
        },

我怀疑该方法是一个承诺,而不是 URL 本身......我该如何解决这个问题?

提前致谢

【问题讨论】:

标签: angularjs kendo-ui


【解决方案1】:

就像已经说过的那样,您需要使用 .then 作为 $http 服务返回一个承诺。你可以试试这样的:

dataSource: {
            transport: {
              read: function (e) {
                CalendarioService.getElencoEventiCalendario()
                  .then(function success(response) {
                  e.success(response.data)
                }, function error(response) {
                  alert('something went wrong')
                  console.log(response);
                })
              }
            }
          }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2014-07-12
    • 2011-07-01
    相关资源
    最近更新 更多