控制器未加载 ui-route 中的状态

controller isnt loading for the state in ui-route

我在 SPA 场景中使用 ui-AngularJS 的路由。 简短描述:有一个一般table 的客户(家庭状态),通过点击购物车标志,用户可以看到客户订单数据。(订单状态)

我有一个 link 旨在将我的状态从 home 更改为 order,同时传递一些参数并且工作正常。我的意思是显示了订单页面,但显然我的订单信息没有加载 tables。

有人可以提示我如何解决吗?谢谢

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'
        });

});

}());

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

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

此问题背后的原因是您正在使用 $routeParams 从 URL 获取参数,这在任何情况下我们都会空白,因为您使用的是 ui-router $stateParams 将具有有关 URL 参数的信息。

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

改为

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