【问题标题】:controller isnt loading for the state in ui-route控制器未加载 ui-router 中的状态
【发布时间】:2015-11-10 12:16:00
【问题描述】:

我将 AngularJS 的 ui-route 用于 SPA 场景。 简短描述:有一个客户总表(家庭状态),通过点击购物车标志,用户可以看到客户的订单数据。(订单状态)

我有一个链接,旨在将我的状态从家庭更改为订单,同时传递一些参数并且工作正常。我的意思是显示了订单页面,但显然我的订单信息没有加载到表格中。

有人可以告诉我如何解决它吗?谢谢

orders.html 页面

    <!-- views/orders.html -->
<div class="container">
  <div class="row" ng-cloack>
    <h2>Orders</h2>
    <br>
    <table class="table table-striped table-hover table-responsive">
      <tr>
        <th>#</th>
        <th>Product</th>
        <th >Total</th>
      </tr>
      <tr ng-repeat="order in orders">
        <td>{{$index + 1 }}</td>
        <td>{{order.product}}</td>
        <td>{{order.total | currency}}</td>
      </tr>
    </table>

</div>

orderController.js 文件

(function() {

var OrdersController = function ($scope, $stateParams) {
      // $routeParams.customerId comes from  routing configuration customerId after PATH       
    var customerId = $stateParams.customerId;
    $scope.orders = null;
    function init() {
        //Search the customers for the customerId
        for (var i=0,len=$scope.customers.length;i<len;i++) {
           if ($scope.customers[i].id === parseInt(customerId)) {
               $scope.orders = $scope.customers[i].orders;
               break;
           }
        }
    }
    $scope.customers = [
        {id:1, joined: '2000-12-02', name:'Ali', city:'Montreal', orderTotal: 9.9956, orders: [ {id: 1, product:'Shoes', total: 9.9956}]},
        {id:2, joined: '1965-01-25',name:'Zoe', city:'Montreal', orderTotal: 19.99, orders: [{id: 2, product:'Baseball', total: 9.995}, {id: 3, product:'Bat', total: 9.9956}]},
        {id:3, joined: '1944-06-15',name:'Tina', city:'Toronto', orderTotal:44.99, orders: [{id: 4, product: 'Headphones', total: 44.99}]},
        {id:4, joined: '1995-03-28',name:'Azad', city:'Vancouver', orderTotal:101.50, orders: [{id: 5, product: 'Kindle', total: 101.50}]}
        ];

    $scope.doSort = function(propName) {
       $scope.sortBy = propName;
       $scope.reverse = !$scope.reverse;
    };
    init();
};

OrdersController.$inject = ['$scope', '$routeParams'];

angular.module('customersApp')
  .controller('OrdersController', OrdersController);

}());

应用模块文件在这里:

(function() {

var app = angular.module('customersApp', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider){

    $urlRouterProvider.otherwise("/")
    $stateProvider
        .state('home',
            {
                url:'/',
                controller:'CustomersController',
                templateUrl:'views/customers.html'
            })
        .state('order',{
                url:'/order/:customerId',
                controller: 'OrdersController',
                templateUrl:'views/orders.html'
        });

});

}());

【问题讨论】:

  • &lt;a ui-sref="order({ customerId: cust.id })" class="color-violet"&gt;&lt;i class="fa fa-shopping-cart"&gt;&lt;/i&gt;&lt;/a&gt; 可以很好地在状态之间切换。

标签: javascript angularjs routing angular-ui-router angularjs-routing


【解决方案1】:

当您使用 ui-router 时,您应该将 $stateParams 注入控制器而不是 $routeParams

注入$routeParams 没有引发错误,因为您的应用中有ngRoute 模块可用。

此问题背后的原因是您使用 $routeParams 从 URL 中获取参数,无论如何我们都会空白,因为您使用的是 ui-router $stateParams 将拥有有关 URL 参数的信息。

OrdersController.$inject = ['$scope', '$routeParams'];

改为

OrdersController.$inject = ['$scope', '$stateParams'];

【讨论】:

  • 控制器仍有错误:即 TypeError: Cannot read property 'customerId' of undefined 并且不让浏览器显示表格
  • 在您发表评论后,我尝试了 OrdersController.$inject = ['$scope', '$stateParams'];,之前奇怪的错误消失了。新的是 TypeError: Cannot read property 'customerId' of undefined 新的。
  • @AliSaberi 你的控制器开始应该是var OrdersController = function ($scope, $stateParams) { 其中$stateParams 将有关于stateParams 的信息
  • 谢谢。它正在工作,现在。这对我来说是一个很大的帮助:)
  • @AliSaberi 很高兴为您提供帮助..谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
相关资源
最近更新 更多