我是Angular.js的初学者,我正在使用Ui-router框架进行路由。 我可以让它工作到我在网址中没有参数的地方。但现在我正在尝试构建一个产品的详细视图,我需要将产品ID传递给URL。
我是通过阅读教程并遵循所有方法来完成的。在教程中,他们使用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"
})
}]
);
}());
这就是我的app.js的样子。我在最后一个州ProdcutDetails上遇到了麻烦。
这是我的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);
});
}
}());
注意:我发现很多人在这里遇到同样的问题https://github.com/angular-ui/ui-router/issues/136,我无法理解他们发布的解决方案,因为我处于一个非常开始的阶段。任何解释都会非常有用。
答案 0 :(得分:1)
控制器定义数组中的参数应为字符串
["ProductResource", "$stateParams"...
这应该可以帮助IoC注入$stateParams
甚至更好:
// 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)
有状态配置
$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"
})
以上使用的功能有一个定义
.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'];
检查here
但你知道什么是真正的问题吗?事实上,只有一条线是麻烦制造者。检查原始状态def:
.state('ProductDetails',{
...
Controller:"ProductDetailController as vm"
})
事实上,唯一重要的变化是
.state('ProductDetails',{
...
controller:"ProductDetailController as vm"
})
即。 controller
而不是 Controller
(开头的资本C)