angular.js - How does the headController controller of angularjs public header get the parameters in the routing configuration?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-15 17:01:23
0
5
671

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

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(5)
小葫芦

Can’t you put the ID in cookies

phpcn_u1582

angular-ui exclusively gets $stateParams

过去多啦不再A梦

Inject $route
and then $route.current.params is

给我你的怀抱

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
});
给我你的怀抱

Inject this service into $routeParams, then

$scope.id = $routeParams.id
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template