I am very beginner in Angular.js, I am using the Ui-router framework for routing. I can make it work upto where I have no parameters in the url. But now I am trying to build a detailed view of a product for which I need to pass the product id into the url.
我对角度很初学者。js,我正在使用Ui-router框架进行路由。我可以让它运行到url中没有参数的地方。但是现在我正在构建一个产品的详细视图,我需要将产品id传递到url中。
I did it by reading the tutorials and followed all the methods. In the tutorial they used resolve to fetch the data and then load the controller but I just need to send in the parameters into the controllers directly and then fetch the data from there. My code looks like below. when I try to access the $stateParams inside the controller it is empty. I am not even sure about whether the controller is called or not. The code looks like below.
我阅读了教程并遵循了所有的方法。在本教程中,他们使用resolve来获取数据,然后加载控制器,但是我只需要将参数直接发送到控制器中,然后从控制器中获取数据。我的代码如下所示。当我试图访问控制器中的$stateParams时,它是空的。我甚至不确定是否调用了控制器。代码如下所示。
(function(){
"use strict";
var app = angular.module("productManagement",
["common.services","ui.router"]);
app.config(["$stateProvider","$urlRouterProvider",function($stateProvider,$urlRouterProvider)
{
//default
$urlRouterProvider.otherwise("/");
$stateProvider
//home
.state("home",{
url:"/",
templateUrl:"app/welcome.html"
})
//products
.state("productList",{
url:"/products",
templateUrl:"app/products/productListView.html",
controller:"ProductController as vm"
})
//Edit product
.state('ProductEdit',{
url:"/products/edit/:productId",
templateUrl:"app/products/productEdit.html",
controller:"ProductEditController as vm"
})
//product details
.state('ProductDetails',{
url:"/products/:productId",
templateUrl:"app/products/productDetailView.html",
Controller:"ProductDetailController as vm"
})
}]
);
}());
this is how my app.js looks like. I am having trouble on the last state, ProdcutDetails.
这就是我的app.js的样子。我在最后一个州有麻烦,ProdcutDetails杂志。
here is my ProductDetailController.
这是我的ProductDetailController。
(function(){
"use strict";
angular
.module("ProductManagement")
.controller("ProductDetailController",
["ProductResource",$stateParams,ProductDetailsController]);
function ProductDetailsController(ProductResource,$stateParams)
{
var productId = $stateParams.productId;
var ref = $this;
ProductResource.get({productId: productId},function(data)
{
console.log(data);
});
}
}());
NOTE : I found lot of people have the same issue here https://github.com/angular-ui/ui-router/issues/136, I can't understand the solutions posted their because I am in a very beginning stage. Any explanation would be very helpful.
注意:我发现很多人在这里都有同样的问题,https://github.com/angular-ui/ui-router/issues/136,我无法理解他们发布的解决方案,因为我还处在起步阶段。任何解释都是很有帮助的。
2 个解决方案
#1
1
I created working plunker here
我在这里创建了工作柱塞
There is state configuration
有状态的配置
$urlRouterProvider.otherwise('/home');
// States
$stateProvider
//home
.state("home",{
url:"/",
templateUrl:"app/welcome.html"
})
//products
.state("productList",{
url:"/products",
templateUrl:"app/products/productListView.html",
controller:"ProductController as vm"
})
//Edit product
.state('ProductEdit',{
url:"/products/edit/:productId",
templateUrl:"app/products/productEdit.html",
controller:"ProductEditController as vm"
})
//product details
.state('ProductDetails',{
url:"/products/:productId",
templateUrl:"app/products/productDetailView.html",
controller:"ProductDetailController as vm"
})
There is a definition of above used features
有一个定义上面使用的特性
.factory('ProductResource', function() {return {} ;})
.controller('ProductController', ['$scope', function($scope){
$scope.Title = "Hello from list";
}])
.controller('ProductEditController', ['$scope', function($scope){
$scope.Title = "Hello from edit";
}])
.run(['$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}])
.controller('ProductDetailController', ProductDetailsController)
function ProductDetailsController ($scope, ProductResource, $stateParams)
{
$scope.Title = "Hello from detail";
var productId = $stateParams.productId;
//var ref = $this;
console.log(productId);
//ProductResource.get({productId: productId},function(data) { });
return this;
}
ProductDetailsController.$inject = ['$scope', 'ProductResource', '$stateParams'];
Check it here
检查在这里
But do you know what is the real issue? Just one line in fact, was the trouble maker. Check the original state def:
但是你知道真正的问题是什么吗?事实上,只有一行是麻烦制造者。检查原始状态def:
.state('ProductDetails',{
...
Controller:"ProductDetailController as vm"
})
And in fact, the only important change was
事实上,唯一重要的改变是
.state('ProductDetails',{
...
controller:"ProductDetailController as vm"
})
I.e. controller
instead of Controller
(capital C at the begining)
即控制器而不是控制器(大写C)
#2
1
The params in controller definition array should be strings
控制器定义数组中的参数应该是字符串
["ProductResource", "$stateParams"...
This should properly help IoC to inject the $stateParams
这将有助于国际奥委会注入$stateParams
And even better:
和更好的:
// the info for IoC
// the style which you will use with TypeScript === angular 2.0
ProductDetailsController.$inject = ["ProductResource", "$stateParams"];
// this will just map controller to its name, parmas are defined above
.controller("ProductDetailController", ProductDetailsController);
#1
1
I created working plunker here
我在这里创建了工作柱塞
There is state configuration
有状态的配置
$urlRouterProvider.otherwise('/home');
// States
$stateProvider
//home
.state("home",{
url:"/",
templateUrl:"app/welcome.html"
})
//products
.state("productList",{
url:"/products",
templateUrl:"app/products/productListView.html",
controller:"ProductController as vm"
})
//Edit product
.state('ProductEdit',{
url:"/products/edit/:productId",
templateUrl:"app/products/productEdit.html",
controller:"ProductEditController as vm"
})
//product details
.state('ProductDetails',{
url:"/products/:productId",
templateUrl:"app/products/productDetailView.html",
controller:"ProductDetailController as vm"
})
There is a definition of above used features
有一个定义上面使用的特性
.factory('ProductResource', function() {return {} ;})
.controller('ProductController', ['$scope', function($scope){
$scope.Title = "Hello from list";
}])
.controller('ProductEditController', ['$scope', function($scope){
$scope.Title = "Hello from edit";
}])
.run(['$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}])
.controller('ProductDetailController', ProductDetailsController)
function ProductDetailsController ($scope, ProductResource, $stateParams)
{
$scope.Title = "Hello from detail";
var productId = $stateParams.productId;
//var ref = $this;
console.log(productId);
//ProductResource.get({productId: productId},function(data) { });
return this;
}
ProductDetailsController.$inject = ['$scope', 'ProductResource', '$stateParams'];
Check it here
检查在这里
But do you know what is the real issue? Just one line in fact, was the trouble maker. Check the original state def:
但是你知道真正的问题是什么吗?事实上,只有一行是麻烦制造者。检查原始状态def:
.state('ProductDetails',{
...
Controller:"ProductDetailController as vm"
})
And in fact, the only important change was
事实上,唯一重要的改变是
.state('ProductDetails',{
...
controller:"ProductDetailController as vm"
})
I.e. controller
instead of Controller
(capital C at the begining)
即控制器而不是控制器(大写C)
#2
1
The params in controller definition array should be strings
控制器定义数组中的参数应该是字符串
["ProductResource", "$stateParams"...
This should properly help IoC to inject the $stateParams
这将有助于国际奥委会注入$stateParams
And even better:
和更好的:
// the info for IoC
// the style which you will use with TypeScript === angular 2.0
ProductDetailsController.$inject = ["ProductResource", "$stateParams"];
// this will just map controller to its name, parmas are defined above
.controller("ProductDetailController", ProductDetailsController);