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

查看:18
本文介绍了如何使用 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.

因此,在调用 state 时,请执行以下操作

So while calling state do something like below

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

将在 URL 下方形成

will form below 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天全站免登陆