Angular UI-Router没有将params传递给服务调用

时间:2015-06-04 16:40:57

标签: angularjs asp.net-web-api angular-ui-router

首先,感谢你的帮助。虽然我试图解决这个问题,但我确实找到了一个角色。我猜我错过了一些明显的东西。我使用ui-router并使用resolve属性来调用资源,并且在我的生命中无法将它传递给Web服务。

首先,我的路线和状态:

(function () {
    "use strict";
    var app = angular.module("MyModule",
        ["ui.router",
         "ngResource",
         "common.services"]);
    app.config(["$stateProvider", '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $urlRouterProvider.otherwise("/");

            $stateProvider.state("home", {
                url: "/",
                templateUrl: "welcomeView.html"
            })
            $stateProvider.state("vehicleList", {
                url: "/vehicles",
                templateUrl: "App/Vehicles/vehicleListView.html",
                controller: "VehicleListController as vm"
            })
            $stateProvider.state("vehicleEdit", {
                url: "/vehicles/edit/:vin",
                templateUrl: "App/Vehicles/vehicleEditView.html",
                controller: "VehicleEditController as vm",
                resolve: {
                    vehicleResource: "vehicleResource",
                    vehicle: function (vehicleResource, $stateParams) {
                        var vehicleVIN = $stateParams.vin;
                        return vehicleResource.get(
                            { vin: $stateParams.vin }).$promise;

                    }
                }
            })
            $stateProvider.state("vehicleDetails", {
                url: "/vehicles/:vin",
                templateUrl: "App/Vehicles/vehicleDetailsView.html",
                controller: "VehicleDetailsController as vm",
                resolve: {
                    vehicleResource: "vehicleResource",
                    vehicle: function (vehicleResource, $stateParams) {
                        var vehicleVIN = $stateParams.vin;
                        return vehicleResource.get(
                            { vin: 'T123432342' }).$promise;

                    }
                }
            })
        }
    ]);
}());

请注意,你看到了几种传递vin的方式,因为我尝试了多种传递变量vehicleVIN,一个字符串等的方法.traceVIN变量确实正确分配了那里注入$ stateParams或传递它的vin似乎不是问题所在。

这是资源:

(function () {
    "use strict";

    angular
        .module("common.services")
        .factory("vehicleResource",
            ["$resource",
             "appSettings",
            vehicleResource]);

    function vehicleResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "/api/Vehicle/:vin", { vin: '@vin' },
            {
                'update': { method: 'PUT' },
                'details': { method: 'GET' },
                'query': { method: 'GET', isArray = true }
            });
    }
}());

这是common.services:

(function () {
    "use strict";

    angular
        .module("common.services", ["ngResource"])
        .constant("appSettings",
        {
            serverPath: "http://localhost:8098"
        });
}());

这是它的服务端(Asp.Net WebAPI):

// GET api/Vehicle
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All, AllowedOrderByProperties = "Name")]
[ResponseType(typeof(IQueryable<IEnumerable<IVehicle>>))]
public IHttpActionResult Get()
{
    return ListOfVehicles...blah..blah
}

// GET api/Vehicle/5
[ResponseType(typeof(IQueryable<IVehicle>))]
public IHttpActionResult Get(string vin)
{
    return SingleVehicle...blah...blah
}

每次执行时,断点都会点击Get(没有vin参数),当我运行fiddler时,它看起来像是在发送一个空体。

有关正在发生的事情的任何想法?非常感谢!

0 个答案:

没有答案