如何使用ui.router模块通过URL将数组传递给AngularJS应用 [英] How to pass array to AngularJS app via url using ui.router module

查看:69
本文介绍了如何使用ui.router模块通过URL将数组传递给AngularJS应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Angular 1.5和ui.router模块,并希望通过int数组向应用抛出URL. www.example.com/#/filter?[1,2,5]或www.example.com/#/filter/[1,2,5]或其他内容.

I use Angular 1.5 and ui.router module and want to pas int array to app throw url. www.example.com/#/filter?[1,2,5] or www.example.com/#/filter/[1,2,5] or something else.

.state('cats', {
             url: '/filter?catsIds', // or /filter/{catsIds}
             templateUrl:'cats.html',
             controller: 'catsCtrl'
         })

目标:

app.controller('catsCtrl',function($stateParams){
    console.log(Array.isArray($stateParams.catsIds)) // goal true
    ...

推荐答案

您不能通过ui-router参数传递对象,我想说的是在传递值时将其与,分隔并分隔.然后通过它.

You can't pass object through an ui-router parameter, I'd say while passing value make it , separated & then pass it.

所以在调用状态时,请执行以下操作

So while calling state do something like below

$state.go('cats', { catsIds: [1,2,5].join(',') })

将在URL下形成

www.example.com/#/filter/1,2,5

$stateParams读取字符串时,确实用,

While reading it from $stateParams do split that string by ,

app.controller('catsCtrl',function($stateParams){
    console.log($stateParams.catsIds.split(','))

这篇关于如何使用ui.router模块通过URL将数组传递给AngularJS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆