Home > Web Front-end > JS Tutorial > body text

AngularJS jumps to page parameter passing method through routing module ui-sref instruction

一个新手
Release: 2017-09-23 09:35:51
Original
3463 people have browsed it

Routing router.js

'use strict';

angular.module('app').config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
  $stateProvider.state('main', {
    url: '/main',
    templateUrl: 'view/main.html',
    controller: 'mainCtrl'
  }).state('position', {
    url: '/position/:id',                  //这里需要传入一个id的参数放在url后面传递过去
    templateUrl: 'view/position.html',
    controller: 'positionCtrl'
  });
  $urlRouterProvider.otherwise('main');
}])
Copy after login

Controllercontroller

<p style="margin-bottom: 7px;">&#39;use strict&#39;angular.module(&#39;app&#39;).controller(&#39;mainCtrl&#39;,[&#39;$scope&#39;,function($scope){<br/>    $scope.list = [{<br/>        id:&#39;1&#39;,                             //将这个id写到页面上<br/>        name:&#39;销售&#39;,<br/>        imgSrc:&#39;image/company-3.png&#39;,<br/>        companyName: &#39;千度&#39;,<br/>        city: &#39;上海&#39;,<br/>        industry: &#39;互联网&#39;,<br/>        time: &#39;2016-06-01 11:05&#39;<br/>    },{<br/>        id:&#39;2&#39;,<br/>        name:&#39;WEB前端&#39;,<br/>        imgSrc:&#39;image/company-1.png&#39;,<br/>        companyName: &#39;慕课网&#39;,<br/>        city: &#39;北京&#39;,<br/>        industry: &#39;互联网&#39;,<br/>        time: &#39;2016-06-01 01:05&#39;<br/>    }];<br/>}]);<br/></p>
Copy after login

html template

<ul class="bg-w position-list">//通过ui-sref="position({id:item.id})"的方式将参数传递过去    
            <li ui-sref="position({id:item.id})" class="item" ng-repeat="item in data">
        <img class="f-l logo" ng-src="{{item.imgSrc}}" alt="">
        <h3 class="title" ng-bind="item.name"></h3>
        <p class="text" ng-bind="item.companyName+&#39;
                [&#39;+item.city+&#39;]&#39;+&#39; &#39;+item.industry"></p>
        <p class="text" ng-bind="item.time"></p>
    </li></ul>
Copy after login

Get route parameters:
Inject $state service, $state service There is a $state.params attribute. This $state.params attribute is a json object. The data contained in this json object is the parameters we passed in earlier.

&#39;use strict&#39;;
angular.module(&#39;app&#39;).controller(&#39;positionCtrl&#39;,[&#39;$q&#39;,&#39;$http&#39;,&#39;$state&#39;,&#39;$scope&#39;,function ($q,$http,$state,$scope) {

              //获取id的参数,并用$http请求对应的数据
              $http.get(&#39;/data/position?id=&#39;+$state.params.id).success(fn1).error(fn2);

}]);
Copy after login

AngularJS cross-page parameter passing method summary:
①Declare url in the route: '/url/:parameter';
②Get data through ui-sref= Mount the parameters behind the url using "url({id:item.id})";
③Inject the $state service into the controller and use the \$state.params attribute to obtain the passed parameters.

AngularJS jumps to page parameter passing method through routing module ui-sref instruction

The above is the detailed content of AngularJS jumps to page parameter passing method through routing module ui-sref instruction. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!