AngularJS:通过控制器之间的参数 [英] AngularJS: pass parameter between controllers

查看:139
本文介绍了AngularJS:通过控制器之间的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的:

我有一个包含若干项(为简单起见让我们假设那些具有​​独特的 ID 名称<一个ListView(或者更确切地说,一个手风琴) / code>)。对于每个项目我想包括一个链接到一个细节视图来显示和编辑属性(除 ID )的项目。

I have a ListView (or rather an accordion) which contains several items (for simplicity lets assume those have a unique id and a name). For each item i want to include a link to a a detail-view to show and edit the properties (except for the id) of that item.

谷歌搜索此返回几个结果一样,例如:<一href=\"http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js\">Passing在角JS控制器之间的数据?

Googling for this returns several results like for example: Passing data between controllers in Angular JS?

和从我了解建议的解决方案包括:

and from what i understand the suggested solutions include:

NG-INIT :(见<一href=\"http://stackoverflow.com/questions/14523679/can-you-pass-parameters-to-an-angularjs-controller-on-creation\">Can你传递参数上建立一个AngularJS控制器?)

我可以在我的列表中使用初始化为每个项目参数 NG-INIT 里面的 NG-重复 。不过现在对这个角的文档提醒:

I could initialize a parameter for each item in my List using ng-init inside of the ng-repeat. However by now the angular-documentation advise against this:

的唯一适当使用ngInit的混叠ngRepeat的特殊性能,如在下面的演示看到。除了这种情况下,你应该使用控制器,而不是ngInit在一个范围内初始化值。

The only appropriate use of ngInit for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

服务

建议使用服务控制器之间共享的全局变量。我已经这样做,用服务来握住我的产品清单,并使用调用该服务添加/删除或更改的项目。我还实施了,我想从我的细节视图控制器调用的getItem(ID)方法。但要做到这一点,我需要先了解该项目的ID!

It is suggested to use services to share global variables between controllers. I am already doing that by using a service to hold my list of items and use calls to the service to add/delete or change the items. I also implemented a getItem(id) method which i would like to call from the controller of my detail-view. But to do that i need to know the id of the item first!

通过 $ routeParams 传递值

passing values via $routeParams

借助角Phonecat教程通过 $ routeParams 。虽然这工作,这意味着我必须包括链接到详细视图我的项目的 ID 。一个用户可以轻易改变的链接,并在一个完全不同的项目的详细视图结束。如果可能的话,我宁愿不暴露内部 ID 中的链接到我的细节视图!

The Angular Phonecat Tutorial solves a similar problem by passing parameters via the $routeParams. While this would work, this means I have to include the id of my item in the link to the detail-view. A user could easily change the link and end up in a detail view for a completely different item. If possible, i would rather not expose the internal id in the link to my detail-view!

这似乎是一个相当普遍的要求。有没有办法,我丢失任何的最佳实践?

This seems like a quite common requirement. Is there any best-practice that i am missing?

推荐答案

您有两个真正的选择(您已经确定):

You have two real alternatives (that you have identified):


  • 使用服务

  • 传递信息作为URL的一部分

两者都是技术上是正确的,可行的,但是从一个语义和状态点我preference是使用的URL。

Both are technically correct and viable but my preference from a semantic and state point of view would be to use a url.

由于您提供它让你链接到的东西是可连接意义上的可点击的链接(即可以通过一个唯一的URL标识的项目或状态)。如果您认为一个开放的手风琴项目更加的状态(而不是一个网页)的一个项目,然后你可以看看UI路由器:的 https://github.com/angular-ui/ui-router

Since you are supplying a clickable link it makes sense that you are linking to something that is linkable (i.e. an item or state that can be identified by a unique url). If you believe an open accordion item to be more an item of state (as opposed to a page) then you could look into ui-router: https://github.com/angular-ui/ui-router

我不知道你为什么不愿意在URL中有项ID。毕竟任何人的兴趣的可以看看与开发工具用户界面的任何信息。如果你不想在UI数据库架构信息,则有混淆ID各种方法来解决这个问题。

I'm not sure why you are averse to having item IDs in the url. After all anyone that's interested can see any information in the UI with developer tools. If you don't want database schema information in the UI then there are various methods of ID obfuscation to address this.

这篇关于AngularJS:通过控制器之间的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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