AngularJS:从视线转移电流状态控制器 [英] AngularJS: Transfer current state from view to controller

查看:123
本文介绍了AngularJS:从视线转移电流状态控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 UI-路由器。我有一个观点,我需要抓住它的当前状态,并在一个按钮发送到控制器作为参数传递给 NG-点击功能。

I'm using UI-Router. I have a view where I need to catch it's current state and send it to a controller as parameter to a ng-click function in a button.

下面的方法返回未定义当我的console.log 功能 transferState

The following approach returned undefined when I console.log the transferred object inside the function transferState

<button ng-click="transferState($state.current.name)">Click me</button>

我该怎么办呢?

推荐答案

的一种常见方法,将喷射两种大量使用服务纳入 $ rootScope 。这是在示例应用程序中使用 - app.js

One common way, would be to inject two heavily used services into $rootScope. This is used in the sample app - app.js

.run(['$rootScope', '$state', '$stateParams',
  function ($rootScope, $state, $stateParams) {

  // It's very handy to add references to $state and $stateParams to the $rootScope
  // so that you can access them from any scope within your applications.For example,
  // <li ng-class="{ active: $state.includes('contacts.list') }"> will set the <li>
  // to active whenever 'contacts.list' or one of its decendents is active.

    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])

现在,每个视图可以直接访问这些。示例应用程序是这里

Now, each view can access these directly. The sample application is here.

这篇关于AngularJS:从视线转移电流状态控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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