Angular best practice (in this situation) for passing data between controllers - service or routeParams?
Date : March 29 2020, 07:55 AM
Does that help I suggest to use a service. If you want stock persistant data, i suggest to use this https://github.com/Zmetser/localstorageservicemyApp.service('controllerSharingData', function() {
var __variables = {};
return {
get: function(varname) {
return (typeof __variables[varname] !== 'undefined') ? __variables[varname] : false;
},
set: function(varname, value) {
__variables[varname] = value;
}
};
});
myApp.controller('IndexCtrl', function($scope, controllerSharingData) {
controllerSharingData.set('toto', 'hello world');
});
myApp.controller('ListCtrl', function($scope, controllerSharingData) {
alert(controllerSharingData.get('toto'));
});
|
Use $routeParams in service
Date : March 29 2020, 07:55 AM
I hope this helps . Because services are singletons. That's why you get that behavior. I recommend that do not inject $routeParams as the init params of a service. Instead, inject it in controllers, then call service's functions using the values in $routeParams as params. factory('Post', function ($resource) {
return {
doPost: function(customerId) {
$resource('api/posts', {customer_id: customerId});
}
};
});
//...
//in some controller
Post.doPost($routeParams.customerId)
|
How am I getting values for routeParams, path and search without injecting $routeParams or $location?
Date : March 29 2020, 07:55 AM
Any of those help Please see a sample code below: , Take a look at the angular documentation, it states:
|
How is the array syntax supposed to work with Angular's $routeParams service?
Date : March 29 2020, 07:55 AM
Hope this helps I'm learning JavaScript. In the course, there is following task: , You need to update .controller('NotesShowController', [function($http, $routeParams) {
.controller('NotesShowController', ['$http', '$routeParams', function($http, $routeParams) {
|
Typescript issue - When using RouteParams I get a type compiler error when trying to get routeParams .id
Date : March 29 2020, 07:55 AM
To fix this issue I made a try and it works for me without any error. Here is what I tried: export class DetailsComponent {
constructor(service:CompanyService, routeParams: RouteParams) {
this.service = service;
this.routeParams = routeParams;
var idMethod1 = this.routeParams.params.id;
var idMethod2 = this.routeParams.get('id'));
(...)
}
}
@RouteConfig([
{ path: '/', component: ListComponent, name: 'Home', useAsDefault: true }
{ path: '/:id/:f', component: DetailsComponent, name: 'Details'}
])
export class AppComponent {
(...)
}
$ tsc -v
message TS6029: Version 1.7.5
|