gpt4 book ai didi

javascript - AngularJS ui-router 解析不返回自定义值

转载 作者:行者123 更新时间:2023-11-30 05:32:04 25 4
gpt4 key购买 nike

在移动到我的页面之前尝试解析一些数据时,我强制解析一个 promise ,将其转换为自定义对象,然后尝试将自定义对象返回到我的解析对象。

            .state("trip.detail", {
url: '/:tripId',
templateUrl: 'partials/trip/detail.html',
controller: 'TripDetailController',
resolve: {
trainData: function (TripService, $stateParams, $q) {
return TripService.getTripById($stateParams.tripId,function (data) {
console.log("received: " + data);
return {
"relativeTrainId": data.trainId,
"from": new Date(data.departure).toISOString(),
"to": new Date(data.arrival).toISOString(),
"projectId": data.projectId,
"isTrip": true,
"tripId": data.id,
"trajectory": data.trajectory,
"statistics": data.statistics
}
}).$promise;
}
}
});

这一切都有效,除了注入(inject)到我的 Controller 中的“trainData”实际上是值“data”,而不是我创建的自定义值。

这是怎么回事?

有关 TripService 的额外信息:

services.factory('TripService', function ($resource) {

function TripService() {
this.tripResource = $resource('rest/trip/:tripId');
}


TripService.prototype.getTrips = function (start, end, project, trainIds, callback) {
return this.tripsResource.query({
projectId: project,
trainIds: trainIds,
from: start,
to: end
}, callback)
}

TripService.prototype.getTripById = function (tripId, callback) {
return this.tripResource.get({
tripId: tripId
}, callback);
}

return new TripService();

});

最佳答案

您必须创建自己的 promise 并使用您的自定义对象解决它:

        .state("trip.detail", {
url: '/:tripId',
templateUrl: 'partials/trip/detail.html',
controller: 'TripDetailController',
resolve: {
trainData: function (TripService, $stateParams, $q) {
var deferred = $q.defer();
TripService.getTripById($stateParams.tripId,function (data) {
console.log("received: " + data);
deferred.resolve({
"relativeTrainId": data.trainId,
"from": new Date(data.departure).toISOString(),
"to": new Date(data.arrival).toISOString(),
"projectId": data.projectId,
"isTrip": true,
"tripId": data.id,
"trajectory": data.trajectory,
"statistics": data.statistics
});
});
return deferred.promise;
}
}
});

关于javascript - AngularJS ui-router 解析不返回自定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com