Issue on access a service in a ui-router controlle

2019-09-11 17:17发布

问题:

I am having issues trying to access a service on a controller. The issue happen when the Ordenes services is called. How I can call a service with two parameter using values from the scope from a controller using ui-router?

I have the same code working but without the use of ui-router. It seems like the code is not loading properly the service inside the Controller.

App.js

'use strict';

app = angular.module('logica-erp', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ui.router',
  'authorization',
  'ui.router.stateHelper', 
  'logica-erp.kds',
  'logica-erp.pos'
])

app.run(function($rootScope) {
  $rootScope.$on("$stateChangeError", console.log.bind(console));
});

app.config(function ($stateProvider, $urlRouterProvider) {
    //delete $httpProvider.defaults.headers.common['X-Requested-With'];
    $urlRouterProvider.otherwise('/');
    $stateProvider
      .state('index', {
        url: '/',
        templateUrl: 'views/main.html',
        controller:'MainCtrl'
      })
      .state('comanda', {
        url: '/comanda',
        templateUrl: 'views/comanda.html',
        controller:'ComandaCtrl'
      })
      .state('counter', {
        url: '/counter',
        templateUrl: 'views/counter.html',
        controller:'CounterCtrl'

      })
  })

comanda.js

(function() {
  'use strict';
  var app;

  app = angular.module('logica-erp.kds', ['timer', 'logica-erp.service.pos']);

  this.ComandaCtrl = [
    '$scope', '$interval', 'Ordenes', function($scope, $interval, Ordenes) {
      var error, stop, success, tick;
      $scope.tiempos = [
        {
          name: '15 seg',
          value: 15000
        }, {
          name: '30 seg',
          value: 30000
        }, {
          name: '60 seg',
          value: 60000
        }, {
          name: '120 seg',
          value: 120000
        }
      ];
      $scope.selected_tiempo = $scope.tiempos[1];
      $scope.tipos = [
        {
          name: 'Alimentos',
          value: 'a'
        }, {
          name: 'Bebidas',
          value: 'b'
        }, {
          name: 'Todos',
          value: ''
        }
      ];
      $scope.selected_tipo = $scope.tipos[2];
      success = function(result) {
        if (angular.toJson(result) !== angular.toJson($scope.ordenes)) {
          $scope.isLoading = true;
          $scope.ordenes = result;
          console.log(JSON.stringify($scope.ordenes));
        }
        return $scope.isLoading = false;
      };
      error = function(error) {
        console.log('error ' + error);
        return $('#modal').foundation('open');
      };
      tick = function() {
        $scope.platos = Ordenes.query({
          tipo: $scope.selected_tipo.value,
          sucursal: 2
        });
        return $scope.platos.$promise.then(success, error);
      };
      tick();
      stop = $interval(tick, $scope.selected_tiempo.value);
      $scope.change_refresh = function() {
        $interval.cancel(stop);
        return stop = $interval(tick, $scope.selected_tiempo.value);
      };
      return $scope.update_order = function(mesa, aaybb_id) {
        return angular.forEach($scope.ordenes.mesas, function(orden) {
          if (orden.mesa === mesa) {
            return angular.forEach(orden.aaybb, function(aaybb) {
              if (aaybb._id === aaybb_id) {
                if (aaybb.estatus === 'ASIGNADO') {
                  aaybb.estatus = 'EN PROCESO';
                } else if (aaybb.estatus === 'EN PROCESO') {
                  aaybb.estatus = 'PREPARADO';
                  $('#timer_' + aaybb._id)[0].stop();
                }
                return Ordenes.update(aaybb);
              }
            });
          }
        });
      };
    }
  ];

  app.controller('ComandaCtrl', ComandaCtrl);

}).call(this);

Console log

Error: value is undefined
extractParams/<@http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:344:11
forEach@http://127.0.0.1:9000/bower_components/angular/angular.js:336:11
extractParams@http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:343:9
ResourceFactory/</Resource[name]@http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:398:39
this.ComandaCtrl</tick@http://127.0.0.1:9000/scripts/controllers/comanda.js:72:25
this.ComandaCtrl<@http://127.0.0.1:9000/scripts/controllers/comanda.js:78:7

回答1:

I fixed the issue, it was a old bug in the angular-resource lib. I didn't know but my bower was installing version 1.0.7 :S anyway; this was very annoying.