Home > Web Front-end > JS Tutorial > 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

黄舟
Release: 2017-05-27 10:35:14
Original
1233 people have browsed it

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";
});
Copy after login

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>
Copy after login


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


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

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 here

  • user
  • . 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;
    });
    Copy after login

    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.

    {{params.age}}

    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!

    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template