The truth is I've done this hundreds of times and it turns out, maybe it's nonsense that I'm not seeing.
My problem is that when I do a $location.path('/Miruta/'+id);
(The url is generated in this way http://localhost/mysite/WebContent/#/alta/minDetalle/2789
) when it reaches the destination url, $routeParams.id
it comes to me as undefined, and I should 2789
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 don't know what I'm doing wrong, I think it's something VM
I'm trying to implement, but honestly I don't know where the error is.
Additionally, I don't see any errors in the console.
You don't have the same number of elements in the
$inject
controller function as you do. Thus, the injected are all misallocated, run so to speak; Well angular doesn't look at the name of the parameters (when you use$inject
) but their order.In other words, what you do when you do
console.log($routeParams.idMinutario);
is actuallyconsole.log($interval.idMinutario);
.. what is surely undefined.Notice...
Solution:
You must add it
$scope
as the first parameter ofMinutarioDetalle
, or remove it from$inject
. Since you don't use it, delete it.Greetings.
Also, in the route you define the parameter as
id
but then you fetch from$routeParams
a parameter calledidMinutario
.