Home  >  Article  >  Web Front-end  >  AngularJS Getting Started Tutorial (2) - Introduction to how to pass parameters in routing

AngularJS Getting Started Tutorial (2) - Introduction to how to pass parameters in routing

黄舟
黄舟Original
2017-05-27 10:35:141216browse

This article mainly introduces the method of AngularJS passing parameters in routing. It analyzes the related skills of AngularJS in implementing routing parameters in the form of examples, and summarizes the relevant operation steps and precautions. Friends in need can refer to the following

The example in this article describes how AngularJS passes parameters in routing. Share it with everyone for your reference, the details are as follows:

Not only can we directly define the value of the attribute in the controller, such as:


app.controller('listController',function($scope){
  $scope.name="ROSE";
});

AngularJS also provides In addition to the function of passing parameters, one way I am currently exposed to is to pass parameters from the view:


<!--首页html-->
<li><a href="#/user/18" rel="external nofollow" rel="external nofollow" >用户</a></li>


//js
.config([&#39;$routeProvider&#39;, function($routeProvider){
    $routeProvider.
    when(&#39;/user/:age&#39;,{
        templateUrl:&#39;list.html&#39;,
        controller:&#39;listController&#39;})
 }]);


<!--list.html-->
<p>
  <p>
  <h1>HI,这里是list.html</h1>
  <h2>{{name}}</h2>
  <h3>{{params.age}}</h3>
</p>
</p>

AngularJs provides a way to pass "18" to the list.html view in the homepage view. That is to put the actual parameter after the view routing address. Such as here25edfb22a4f469ecb59f1190150159c6f3070c630e05965d2637a2d3f8fe3ef6user5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb. Then declare the variable in the when method of JS to match the actual parameter. But the actual parameter is stored in $routeParams (array) as a "key value", and we must inject it in the control symbol (the so-called injection actually means sharing all the attributes and values ​​​​in it?). Then declare and assign the value in the controller (that is, take it out). As follows:


.controller(&#39;listController&#39;,function($scope,$routeParams){
  $scope.name="ROSE";
  $scope.params=$routeParams;
});

Summarize the steps of passing parameters as follows:

1. Add after "/" in the home page view The actual parameters to be passed.
2. Define a variable in the routing path in the routing configuration for matching, in the format /:varible.
3. Configure the controller and inject $routeParams into the controller.
4. Assign values ​​in the controller. $scope.params=$routeParams; .
5. The actual parameter is successfully displayed in the view after routing is completed. 684271ed9684bde649abda8831d4d355{{params.age}}39528cedfa926ea0c01e69ef5b2ea9b0

One thing to note is that the actual parameter is stored as a key value in $routeParams , the value must be obtained by accessing its corresponding variable (age in this case).

The above is the detailed content of AngularJS Getting Started Tutorial (2) - Introduction to how to pass parameters in routing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn