search

Home  >  Q&A  >  body text

angular.js - How does the headController controller of angularjs public header get the parameters in the routing configuration?

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:

曾经蜡笔没有小新曾经蜡笔没有小新2794 days ago672

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-05-15 17:03:23

    Can’t you put the ID in cookies

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:03:23

    angular-ui exclusively gets $stateParams

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:03:23

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

    reply
    0
  • 给我你的怀抱

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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-15 17:03:23

    Inject this service into $routeParams, then

    $scope.id = $routeParams.id
    

    reply
    0
  • Cancelreply