angular.module('demoapp').controller("EventControllerNearPosition", EventControllerNearPosition);
EventControllerNearPosition.$inject = ["$scope", "ApiEventFactory", "ApiPositionFactory", "UserInteractionMessagesFactory", "$location"]
function EventControllerNearPosition($scope, apiEvent, apiPosition, UIMfactory, $location){
UIMfactory.printUserSuccessMessages();
UIMfactory.printUserFailedMessage();
getAllPositions();
function getAllPositions() {
apiPosition.getAllPositions().then(function(data){
$scope.positions = data.requested_positions;
}).error(function(error){
UIMfactory.addUserFailedMessage("Something went wrong, try again!");
$location.path("/");
});
};
$scope.SearchEvent = function(){
apiEvent.showNearbyEvents($scope.position_id).then(function(nearEvents){
$scope.events = nearEvents;
}).error(function(error){
UIMfactory.addUserFailedMessage("Something went wrong when fetching the events, try again!");
$location.path("/");
});
};
};
角度代码^
<div class="event-container">
<div>
<h3>Ange position</h3>
<select id="position" data-ng-model="position_id">
<option data-ng-repeat="position in positions" value="{{position.id}}">
{{position.location_name}}
</option>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default" data-ng-click="SearchEvent()" value="Skicka"/>
</div>
选择不会绑定我从 i api 获得的位置。我已经调试了应用程序并检查了我是否返回了正确的值。我知道相似或"相等"的代码在其他部分视图中有效,所以我不明白为什么它在这里不起作用。而且当我按下按钮时,它将触发"SearchEvent",调试器中没有任何反应。如果这很重要,我使用火狐
感谢您的任何反馈!
我只能看到一个错误,您应该从.then
函数的响应对象中获取data
。
function getAllPositions() {
apiPosition.getAllPositions().then(function(response){
//change in below line
$scope.positions = response.data.requested_positions;
}, function(error){
UIMfactory.addUserFailedMessage("Something went wrong, try again!");
$location.path("/");
});
};
能看到你得到的响应数据,我可以很好地判断。但就这个代码片段而言,.then()
没有得到很好的使用。.then 方法的工作方式.then(RESPONSE FUNCTION, ERROR FUNCTION)
没有必要使用.error()
。
function getAllPositions() {
apiPosition.getAllPositions().then(function(response){
$scope.positions = response.data.requested_positions;
},function(error){
UIMfactory.addUserFailedMessage("Something went wrong, try again!");
$location.path("/");
});
};
我已经解决了我的问题。首先,我删除了response.requested_position中的"s";
然后我用了Pankaj Parkar提交的answear 。除了我没有在response.data.requested_positions中使用"数据",因为我使用"$resource"而不是"$http"或任何它的名字!
感谢您的所有反馈!!