在 ui-router 控制器中访问服务的问题 [英] Issue on access a service in a ui-router controller

查看:22
本文介绍了在 ui-router 控制器中访问服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试访问控制器上的服务时遇到问题.调用 Ordenes 服务时会发生此问题.如何使用 ui-router 使用控制器范围内的值调用具有两个参数的服务?

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?

我有相同的代码,但没有使用 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);

控制台日志

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

推荐答案

我修复了这个问题,这是 angular-resource 库中的一个旧错误.我不知道,但我的凉亭正在安装 1.0.7 版:S;这很烦人.

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.

这篇关于在 ui-router 控制器中访问服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆