AngularJS/ui-router:$state.go 在 ng-click 中不起作用 [英] AngularJS / ui-router: $state.go does not work inside ng-click

查看:23
本文介绍了AngularJS/ui-router:$state.go 在 ng-click 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,其中包含以下代码:

I have a view where I have the following code:

<input type="button" value="New Post" ng-click="$state.go('blog.new-post')">

目标是在无需使用 href 的情况下过渡到新状态.不幸的是,上面的代码没有触发.

The goal is to transition to a new state without having to use href. Unfortunately the code above just does not fire.

我还尝试在此视图的控制器中包含 $state:

I also tried to include $state in the controller for this view:

app.controller('blogPostsController', function($scope, $stateParams, $http, $state) ...

但还是没有.transictionTo 似乎也不起作用.

But still nothing. transictionTo also does not seem to work.

有人知道如何使这项工作有效吗?

Anyone has any idea on how to make this work?

我只能通过分配来使它工作:

I could only make it work by assigning:

$scope.$state = $state;

在我的控制器里面.这看起来很丑.真的没有其他方法可以在不将其分配给作用域的情况下访问 $state 吗?

inside my controller. This seems ugly. There is really no other way to access $state without assigning it to scope?

推荐答案

根据 https://github.com/angular-ui/ui-router/wiki/Quick-Reference#note-about-using-state-within-a-模板,你可以将它添加到 $rootScope,这样它就可以在所有的范围和所有的模板中使用,通过

According to https://github.com/angular-ui/ui-router/wiki/Quick-Reference#note-about-using-state-within-a-template you can add it to $rootScope, so it will be available in all the scopes and hence all the templates, by

angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
  $rootScope.$state = $state;
  $rootScope.$stateParams = $stateParams;
});

这篇关于AngularJS/ui-router:$state.go 在 ng-click 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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