AngularJS 不同页面之间的传参

1、基于ui-router传参

① 定义的跳转路由中:

.state('faceWarning',{
            url: '/faceWarning',
            templateUrl: 'pages/face/faceWarning.html',
            controller: 'WarningCtrl'
        })
.state('faceWarningList',{
            url: '/faceWarningList/:groupId',
            templateUrl: 'pages/face/faceWarningList.html',
            controller: 'WarningListCtrl'
        })

②传参的页面html中

ng-click="jumpWarningList(data.groupId)"
定义页面跳转函数 (使用ui-router的$state.go接口):
faceApp.controller('WarningCtrl', ['$rootScope', '$scope', '$state',
    function($rootScope, $scope, $state) {
         $scope.jumpWarningList = function(groupId) {
            $state.go('faceWarningList', {groupId: groupId});
        };
  }]);

对于a链接跳转,对于angularjs 则在页面中

③接受的页面js中,通过ui-router的$stateParams获取参数groupId

faceApp.controller('WarningCtrl', ['$rootScope', '$scope', '$stateParams',
    function($rootScope, $scope, $stateParams) {
         $scope.searchSpecial.groupId = $stateParams.groupId;
  }]);

这种跳转,在不传递参数时,url: '/faceWarningList/:groupId', 这里将会影响单独跳转的实现,导致点击无效。

如果要实现单独点击跳转(主页面不带参数跳转),需要在app.js中定义参数,不然$state.go在传输之后在目标controller接受的时候会被filter过滤掉

.state('faceWarningList',{
            url: '/faceWarningList',
            templateUrl: 'pages/face/faceWarningList.html',
            params: {'groupId': null},
            controller: 'WarningListCtrl'
        })

https://www.cnblogs.com/miny-simp/p/7910069.html

 

2、angular获取通过链接形式访问的页面,要获取url中的参数,就不能通过路由的方式传递获取。应该使用$location获取url值,通过url传参

var para=$location.$$search["para"]

// 带#号的url,看?号的url,见下面  
url = http://qiaole.sinaapp.com?#name=cccccc  
  
$location.absUrl();  
// http://qiaole.sinaapp.com?#name=cccccc  
  
$location.host();  
// qiaole.sinaapp.com  
  
$location.port();  
// 80  
  
$location.protocol();  
// http  
  
$location.url();  
// ?#name=cccccc  
  
// 获取url参数  
$location.search().name;  
// or  
$location.search()['name'];  
  
// 注:如果是这样的地址:http://qiaole.sinaapp.com?name=cccccc  
  
var searchApp = angular.module('searchApp', []);  
searchApp.config(['$locationProvider', function($locationProvider) {  
  $locationProvider.html5Mode(true);  
}]);  
searchApp.controller('MainCtrl', ['$scope', '$location', function($scope, $location) {  
  if ($location.search().keyword) {  
    $scope.keyword = $location.search().keyword;  
  }  
}]);

https://www.cnblogs.com/xyyt/p/6991837.html

3、a链接跳转传参

html页面

<a href="#/faceWarningList/{{groupId}}" ng-click="groupId = 1">

路由配置

.state('faceWarningList',{
            url: '/faceWarningList/:groupId',
            templateUrl: 'pages/face/faceWarningList.html',
            controller: 'WarningListCtrl'
        })

在跳转过去的页面中,只能用html原生方法获取url值并解析

var locationUrl = windows.location.href;
var groupId = locationUrl.slice(-1);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值