Angularjs $ routeParams gives me undefined

0

I've really done this a hundred times and it turns out to me, capable is a nonsense that I'm not seeing.

My problem is that when I do $location.path('/Miruta/'+id); (The url is generated in this way http://localhost/mysite/WebContent/#/alta/minDetalle/2789 ) when it reaches the destination URL, the $routeParams.id comes to me as undefined, and it should be 2789 I leave the code.

//Parte del App.js
app
  .config([
    '$routeProvider',
    '$locationProvider',
    '$httpProvider',
    function($routeProvider, $locationProvider, $httpProvider) {

      $httpProvider.interceptors.push('sacgHttpInterceptor');

      $routeProvider

      .when("/alta/minDetalle/:id", {
        templateUrl: "pages/altas/mindetalle.html",
        controller: 'MinDetalleCtrl',
        controllerAs: 'vm'
      })
      .otherwise({
          templateUrl: "pages/home.html",
          controller: "homeCtrl"
        });
}]);


//Registro.js
//Llamo mi servicio de guardar

  ObjectFactory.save(min).then(function(data){
    console.log('Salvado!!!');
    var id = data.idMinutario;
    resetDropDown();
    $location.path('/alta/minDetalle/'+id);
  });

//Js donde redirecciono.
(function(){
  'use strict';

  /*
   * Para mejor lectura del codigo es mejor colocarlo de esta forma (Nombre
   * cotrolador, Nombre de funcion)
   */
  app.controller('MinDetalleCtrl', MinDetalle);

      //  /* Inyecto las dependencia que va a necesitar mi funcion main */
 MinutarioDetalle.$inject = ['$scope', '$rootScope', '$http', 'ObjectFactory', "$location", 'DTOptionsBuilder', '$filter', '$q', "ObjectFactoryPersistence", "$interval", '$routeParams'];

  ////Funcion de mi controller
  function MinutarioDetalle($rootScope, $http, ObjectFactory, $location, DTOptionsBuilder, $filter, $q, ObjectFactoryPersistence, $interval, $routeParams){

    ///Asignamos this to vm (ViewModel) Porque amamos los VM :D

  console.log("llegue bien");
  console.log($routeParams.id); //undefined

    var vm = this;
    vm.hideToggle = true;
    vm.hideToggleDocumentos = true;
    vm.hideToggleAnexos = true;


    vm.testCode = "Hellos World";

    vm.click = clickme;

    function clickme(){
      alert('test');
    }
  }

})();

I do not know what I'm doing wrong, I think it's something of the VM I'm trying to implement, but I honestly do not know where the error is.

Additionally, I do not see any errors in the console.

    
asked by Wilfredo 11.03.2016 в 02:00
source

2 answers

3

You do not have the same number of elements in $inject as in the function of the controller. Thus, the injected ones are all badly assigned, running as it were; For angle does not look at the name of the parameters (when you use $inject ) but its order.

That is, what you do when you do console.log($routeParams.idMinutario); is actually console.log($interval.idMinutario); .. which is probably undefined.

Look ...

MinutarioDetalle.$inject = ['$scope', '$rootScope', '$http', 
              'ObjectFactory', "$location", 'DTOptionsBuilder', 
              '$filter', '$q', "ObjectFactoryPersistence", 
              "$interval", '$routeParams']; // 11 parametros

////Funcion de mi controller
function MinutarioDetalle($rootScope, $http, ObjectFactory, 
              $location, DTOptionsBuilder, $filter, 
              $q, ObjectFactoryPersistence, $interval, 
              $routeParams){ // 10 parametros

Solution:

You must hit $scope as first parameter of MinutarioDetalle , or delete it from $inject . Since you do not use it, delete it.

Greetings.

    
answered by 11.03.2016 / 04:46
source
1

Also, in the path you define the parameter as id but then you collect from the $routeParams a parameter called idMinutario .

    
answered by 11.03.2016 в 09:22