Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 为什么这个有角度的ui路由器代码会在$digest中导致无限循环?_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs 为什么这个有角度的ui路由器代码会在$digest中导致无限循环?

Angularjs 为什么这个有角度的ui路由器代码会在$digest中导致无限循环?,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我已经尽我所能把代码写下来了。有关嵌套状态和事件处理/广播的某些信息正在导致无限循环。在Chrome中,我可以暂停它,并看到它在Angular的$digest函数中永远循环。知道为什么吗?它是我的示例代码中的一个bug,还是 angular.module('bugapp',['ui.router'])) .run(函数($rootScope、$state、$stateparms){ $rootScope.$state=$state; $rootScope.$stateParams=$state

我已经尽我所能把代码写下来了。有关嵌套状态和事件处理/广播的某些信息正在导致无限循环。在Chrome中,我可以暂停它,并看到它在Angular的
$digest
函数中永远循环。知道为什么吗?它是我的示例代码中的一个bug,还是


angular.module('bugapp',['ui.router']))
.run(函数($rootScope、$state、$stateparms){
$rootScope.$state=$state;
$rootScope.$stateParams=$stateParams;
})
.config(函数($locationProvider、$stateProvider、$urlRouterProvider){
$locationProvider.html5Mode(false);
$stateProvider
.state(“根”{
摘要:没错,
url:“/servletContext?asUser”,
模板:“”/???
})
.state(“root.home”{
摘要:没错,
网址:“/home”,
模板:“”
})
.state(“root.home.profile”{
url:“/profile”,
模板:“随便什么”
})
})
.controller('BugAppCtrl',函数($scope、$state、$stateparms、$log、$location){
$log.log('BugAppCtrl:constructor');
$scope.hasData=函数(){
var res=!!$scope.foo;
//$log.log(“hasdatacalled,returing”+res+“foo是”+$scope.foo);
返回res;
};
$scope.$on(“$stateChangeSuccess”,函数(){
$log.log(“状态已更改!(改为“+$State.current.name+”);
$scope.foo=‘垃圾’;
$scope.$broadcast(“resetfoo”);
});
$state.go('root.home.profile');
});

我怀疑这是UI路由器中的一个错误,原因有两个:

  • 我尝试了你的代码,然后尝试降级UI的版本 路由器到0.2.7。当我使用0.2.7时,它起作用了
  • 即使您继续使用0.2.8版的UI路由器,如果您通过$location而不是$state执行状态更改,它也可以工作。下面是使用$location而不是$state.go调用的示例:

    $location.path('/servletContext/home/profile');
    
  • 尽管我使用并推荐UI Router(没有嵌套视图不行),但如果将来发现用户试图转到某些页面时希望执行任何类型的拦截或重定向,我建议使用$location.path而不是$state,原因如下所述

    编辑:我以前没有尝试过使用参数,只是尝试了使用您发布的代码(我创建了第二个控制器,并将其分配给您的“root.home.profile”状态),它可以正常工作。来自UI路由器的指令如下所示。但基本上,如果您在状态定义中设置URL的方式与UI路由器相同:

    url: "/profile/:foo",
    
    然后在$location.path调用中,在路径中添加参数:

    $location.path('/servletContext/home/profile/12');
    
    您可以从访问控制器中的12

    $stateParams.foo
    

    这很有帮助。当您使用$location而不是$state时,您是否以这种方式成功地转换到具有参数的状态?看起来是可能的-我尝试过并在回答中添加了信息。对我来说,位置路径也是一样,我发出一些事件事件,至少让UI路由器构造您将要更改的位置更可取,aka:
    $location.path($state.href('my.state.name',{foo:'bar');
    $stateParams.foo