For example:
There are three controllers in the application, they are in a horizontal relationship
Controller A controls the head part of the page
Controllers B and C control the details page, edit page, routing The relationship is as follows
when('/detail/:id', { templateUrl: 'tpls/detail.html', controller: "BController" }).
when('/edit/:id', { templateUrl: 'tpls/edit.html', controller: "CController" }).
The current requirement is to obtain the parameter id in the routing URL in controller A. How to obtain it?
My current implementation:
I wrote a service and wrote a function in the service. The function of this function is to use $location.path() to get the current URL, and then use the string The interception method is used to get the id in the parameter. Although the value is obtained, it feels that it should not be this way
My attempt:
$location has an API, $location.search().name is used to get the current url parameters, but this API cannot get my parameters because my url is in the shape of index.html# In the form of /detail/1, it cannot be obtained without name
Another idea is to add a higher-level controller to the three controllers and transfer it through the parent controller, but this seems inappropriate
给我你的怀抱2017-05-15 17:03:23
http://stackoverflow.com/questions/25370775/exposing-the-current-state-name-with-ui-router
//this watches for state changes
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
//do something when state changes
$yourService.state = toState.name;
$yourService.postid = toParams.postid;
console.log(toParams); //this is the stateParams you need
});
给我你的怀抱2017-05-15 17:03:23
Inject this service into $routeParams, then
$scope.id = $routeParams.id