请参阅此plunker:https://run.plnkr.co/mTN6nIsUkxFtiZLx/#/state1
https://plnkr.co/edit/KYQG3OMPoCthEDcus12Y?p=info
它基本上是UI路由器文档中提供的演示代码。为了以编程方式测试状态之间的导航,我创建了使用$state.go(stateName)
和$location.path(stateUrl)
的控制器函数,它们都可以正常工作。
控制器的代码:
myApp.controller('mainController', ['$scope', '$state', '$location', function($scope, $state, $location) {
$scope.usingState = function() {
console.log('usingState() called');
$state.go('state2');
};
$scope.usingLocation = function() {
console.log('usingLocation() called');
$location.path('/state2');
};
}]);
但是,在交互式控制台(开发工具)中,它不起作用。如果我执行以下操作:
$state = angular.element(document).injector().get('$state');
$state.go('state2');
或
$location = angular.element(document).injector().get('$location');
$location.path('/state2');
什么都没发生。为什么?
我希望能够在交互式控制台中进行快速测试......
答案 0 :(得分:2)
您提到的上述代码似乎是在控制台上运行的。在plunker中,您无法从文档中获取injector
对象,因为plunker在<iframe>
内运行代码,在其中加载角度。
测试功能打开UI路由器Sample App。在控制台中输入以下代码并观察状态转换:
angular.element(document).injector().get('$state').go('about');
<强>更新强>
在您提供的plunker中,我已将角度版本 1.1.5 更改为 1.2.0 (在 1.1.5之后发布的版本强大的>)一切似乎都很好。以下是1.2.0-rc.3和1.2.0的更改日志,可能有助于您了解其在以前版本中无效的原因。
Updated Plunker。希望这有帮助!