Angularjs 我可以设置ui路由器状态';数据属性的动态变化?
假设我有这样一个州定义:Angularjs 我可以设置ui路由器状态';数据属性的动态变化?,angularjs,angular-ui-router,state,router,Angularjs,Angular Ui Router,State,Router,假设我有这样一个州定义: $stateProvider.state("main", { url: "main/:param/list", controller: "MainController", data: function($stateParams) { if($stateParams.param === "A") { return 'param1'; } else if($stateParams.param === "B") {
$stateProvider.state("main", {
url: "main/:param/list",
controller: "MainController",
data: function($stateParams) {
if($stateParams.param === "A") {
return 'param1';
} else if($stateParams.param === "B") {
return 'param2';
}
}
}
我可以这样设置数据属性吗?是否有任何方法可以实现所需的行为?数据对象是要添加到$state服务的任何数据的存储,这意味着您可以将函数存储在其中,但不会执行它,我假设这不是您试图实现的,您可以了解如何将自定义数据添加到$state ,因此,要执行您想要执行的逻辑,您必须使用控制器而不是$state服务,如下所示:
$stateProvider.state("main", {
url: "main/:param/list",
controller: "MainController"/*,
//only if you want to "store" your data on the $state, u can use it like this
data: {
myCustomParam: 'my Awesome Value'
}*/
})//..all your other states
//now you pass the parameter in the url like This
//http://xxxxxxx/main/myparamvalue/list
//and in the controller
app.controller('MainController',function($state, $stateParams){
//Here you do your Logic
if($stateParams.param === "myparamvalue") {
console.log('param1');//it should go here
} else if($stateParams.param === "anythingelse") {
console.log('param2');
}
/*if you have any values to get from the state data as previously mentioned you can get it like this*/
console.log($state.current.data.myCustomParam);//output: my Awesome Value
});
你认为这是一个有效的问题吗??如果您定义了param1,param2??很抱歉,我知道您的意思,我将编辑问题“为什么否决投票?”??是复制品吗?糟糕的陈述?你为什么不在下面发表评论来帮助改进呢?