AngularJS/ui-router : $state. go 在 ng-click 中不起作用

标签 angularjs angular-ui-router

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

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

目标是在不必使用 href 的情况下转换到新状态。不幸的是,上面的代码没有触发。

我还尝试在此 View 的 Controller 中包含 $state:
app.controller('blogPostsController', function($scope, $stateParams, $http, $state) ...

但还是什么都没有。 transictionTo 似乎也不起作用。

任何人都知道如何使这项工作?

编辑:我只能通过分配使其工作:
$scope.$state = $state;

在我的 Controller 里面。这看起来很丑。真的没有其他方法可以访问 $state 而不将其分配给作用域吗?

最佳答案

如果您正在使用 ui-router有一个名为 ui-sref 的属性可以按如下方式使用:

<a ui-sref="blog.new-post"></a>

我建议使用它并将链接样式设置为按钮(如果您使用像 Twitter Bootstrap 这样的 CSS 库,则可以轻松完成)。这样您在编写链接时就不需要处理任何 JavaScript。

如果需要向状态传递参数,可以执行以下操作:
<a ui-sref="blog.edit-post({id: item.id})"></a>

其中 $scope.item 是一个具有 ID 属性的对象,您希望将其作为 URL 参数传递。

关于AngularJS/ui-router : $state. go 在 ng-click 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20637471/

相关文章:

angularjs - angularjs 中 `&lt;input&gt;` 元素上的 ng-type/show-password 指令

javascript - Angular UI 路由器状态导航

AngularJS Ui-Router 嵌套 View 不适用于 html5mode

javascript - 在不删除 Angularjs 中的历史记录的情况下更改 URL

javascript - Angular + typescript : Use Directive Class Scope Variable in Controller Class

angularjs - 如何在 angularJS 中取消订阅广播事件。如何删除通过 $on 注册的函数

javascript - ionic 收集重复不起作用

javascript - ng-options 从字典传递值

angularjs - 在 ionic 下调试 Angular ui-router

javascript - Foundation for Apps,如何更改表单提交的 View ?