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

前端之家收集整理的这篇文章主要介绍了AngularJS / ui-router:$state.go在ng-click中不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个视图,我有以下代码
<input type="button" value="New Post" ng-click="$state.go('blog.new-post')">

目标是转换到新的状态,而不必使用href.不幸的是,上面的代码不会触发.

我还试图在控制器中包含$state以查看此视图​​:

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

但依然没有. transiction也似乎不起作用.

任何人有什么想法如何使这项工作?

编辑:我只能通过分配:

$scope.$state = $state;

在我的控制器内这似乎很丑没有其他方式可以访问$state而不分配给范围?

根据 https://github.com/angular-ui/ui-router/wiki/Quick-Reference#note-about-using-state-within-a-template,您可以将其添加到$rootScope中,因此它将在所有范围内可用,因此所有模板都可用
angular.module("myApp").run(function ($rootScope,$state,$stateParams) {
  $rootScope.$state = $state;
  $rootScope.$stateParams = $stateParams;
});

猜你在找的Angularjs相关文章