Angular Ui-router 无法访问我的控制器内的 $stateParams [英] Angular Ui-router can't access $stateParams inside my controller

查看:28
本文介绍了Angular Ui-router 无法访问我的控制器内的 $stateParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular.js 的初学者,我正在使用 Ui-router 框架进行路由.我可以让它在 url 中没有参数的地方工作.但现在我正在尝试构建一个产品的详细视图,我需要将产品 ID 传递到 url 中.

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.

我是通过阅读教程并遵循所有方法来做到的.在教程中,他们使用 resolve 来获取数据,然后加载控制器,但我只需要直接将参数发送到控制器中,然后从那里获取数据.我的代码如下所示.当我尝试访问控制器内的 $stateParams 时,它是空的.我什至不确定控制器是否被调用.代码如下所示.

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.

(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 上遇到问题.

this is how my app.js looks like. I am having trouble on the last state, ProdcutDetails.

这是我的 ProductDetailController.

here is my 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,我无法理解他们发布的解决方案,因为我还处于起步阶段.任何解释都会非常有帮助.

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.

推荐答案

我在这里创建了工作plunker

有状态配置

$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'];

此处

但是你知道真正的问题是什么吗?事实上,只有一行是麻烦制造者.检查原始状态def:

But do you know what is the real issue? Just one line in fact, was the trouble maker. Check the original state def:

.state('ProductDetails',{
    ...
    Controller:"ProductDetailController as vm"
})

事实上,唯一重要的变化是

And in fact, the only important change was

.state('ProductDetails',{
    ...
    controller:"ProductDetailController as vm"
})

controller 而不是 Controller(开头的大写 C)

I.e. controller instead of Controller (capital C at the begining)

这篇关于Angular Ui-router 无法访问我的控制器内的 $stateParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆