Angular stateParams to controller,$ urlRouterProvider [英] Angular stateParams to controller, $urlRouterProvider
问题描述
我正在使用Ionic和AngularJS构建应用程序。
I'm building an app with Ionic and AngularJS.
现在我有一个包含项目列表的页面。每个项目都有一个ID,因此点击会产生如下结果:
Now I have a page with a list of items. Each item has an ID, and so a click results in something like this:
/item/1
或
/item/2
其中1和2是ID。
现在我有这样的默认状态,对于包含此列表的主页:
Now I have the default state like this, for the homepage with this list:
.state('app.home', {
url: "/home/",
views: {
'menuContent': {
templateUrl: "app/home.html"
},
},
controller: 'HomeCtrl'
})
我想要的是将参数传递给控制器。我发现的是以下解决方案,但这不起作用。原因是它不知道使用哪个控制器(在上面的例子中,指定了 HomeCtrl
。)。
What I want is to pass the paramater to the controller. What I found is the following solution, but this isn't working. The reason is that it doesn't know which controller to use (in above example, HomeCtrl
is specified.).
.state('app.item', {
url: "/item/:id",
views: {
'menuContent': {
templateUrl: "app/item.html"
},
},
controller: function($scope, $stateParams) {
$scope.id = $stateParams.id;
}
})
如你所见 controller
现在有一个函数,但是我在哪里指定要使用的实际控制器?
此外,这是正确的方式还是有更好/更简单的方式?
As you can see controller
now has a function, but where do I specify the actual controller to use?
Also, is this the right way or is there a better/easier way?
推荐答案
.controller('controllerName',
[ '$scope','$stateParams'
function($scope , $stateParams ) {
//
$scope.id = $stateParams.id;
在$ stateParams.id中,id将是你参数的名称在url(:id)中定义。
In $stateParams.id , id will be name of parameter you define in url(:id).
这篇关于Angular stateParams to controller,$ urlRouterProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!