我怎样才能将id传递给我的ui-view

时间:2014-03-07 13:43:21

标签: javascript angularjs api angular-ui-router

我想通过ID访问广告资源,使用链接/inventory/description/:{{id}}但它不起作用,什么都没有显示。我怎样才能通过id访问它?

app.config(function config( $stateProvider, $urlRouterProvider) {
  $stateProvider.state('inventory',{
    url:'/inventory',
        views: {
            "main": {
                controller: 'InventoryCtrl',
                templateUrl: 'inventory/main.tpl.html'
            }
        },
        data:{ pageTitle: 'Inventory' }
  }
  ).state('inventory.detailview',{
    url:'/inventory/detailview',
        views: {
            "detailview": {
                controller: 'InventoryCtrl',
                templateUrl: 'inventory/detail.tpl.html'
            }
        },
        data:{ pageTitle: 'DetailView' }
  }
  ).state('inventory.description',{
    url:'/inventory/description/:{{id}}',
        views: {
            "descriptionview": {
                templateUrl: 'inventory/description.tpl.html',
                controller: function($scope, Inventory){
                    $scope.id = Inventory.query('id');

            }}
        },
        data:{ pageTitle: 'DescriptionView'}
  });
});

我的工厂

app.factory('Inventory', function($resource, $http) {
return $resource('http://web.lv/api/v1/inventory/:id', {id: "@id"},
    {
        update: {
            method: 'POST',
            params: {id: '@id'},
            isArray: false
        },
        save: {
            method: 'PUT'
        },
        query: {
            method: 'GET',
            params: {id: '@id'},
            isArray: false
        },
        create: {
            method: 'POST'
        },
        drop: {
            method: 'DELETE',
            params: {id: "@id"}
        }
    }
);
});

我的控制器

app.controller('InventoryCtrl', function($scope, $http,  Inventory, $location) {    

    //getting the objects from Inventory
        $scope.info = Inventory.query();

    //
        $scope.id = Inventory.query('id');

}

1 个答案:

答案 0 :(得分:0)

如果您使用的是ui-router,则可以使用stateParams

app.controller('InventoryCtrl', function($scope,$stateParams,Inventory) { 
     $scope.id = $stateParams.id;
}