2

谁能帮我集成状态机来控制路由?

最好的方法是什么?创建服务?

我需要基本上拦截每个 $location 请求,运行状态机并让它找出下一个 $location.path 应该是什么。

把这个问题想象成一堆随着时间的推移而被添加和删除的问题。用户偶尔访问一次,将用户的答案对象传递给状态机,状态机确定要加载哪个问题。这是我的伪代码,但我需要弄清楚把它放在哪里或者我可以挂钩什么事件,以确保所有路由请求都通过机器。我需要特定的 stateMachine 控制器吗?我要创建服务吗?我在哪里使用该服务?我需要覆盖 $locationProvider 吗?

$scope.user.answers = [{

id: 32,
answer: "whatever"

},
{

id:33,
answer: "another answer"

}]

$scope.questions = [{

id:32, 
question:"what is your name?", 
path:"/question/1"

},{

id:34, 
question:"how old are you?", 
path:"/question/2"

}]

var questions = $scope.questions;

angular.forEach(questions, function(question) {

if(question.id !exist in $scope.user.answers.id) {

$location.path = question.path
break;

});

谢谢

4

2 回答 2

7

你有没有研究过这个项目?

https://github.com/angular-ui/ui-router

我才刚刚开始尝试,但它看起来应该可以满足您的需求。

于 2013-03-18T02:45:30.080 回答
2

与其拦截 $location 更改,不如使用 ng-click 和ng-include?使用 ng-click 调用您的状态机逻辑,并让它更新模型/范围属性,该属性指定要通过 ng-include 加载的模板:

<a ng-click="runStateMachine()">process answers</a>
<div ng-include src="partialToInclude"></div>

控制器:

$scope.runStateMachine() {
    ... process $scope.answers and set $scope.partialToInclude  ...
}
于 2012-12-30T04:35:11.533 回答