Angularjs Angular$scope.model无法绑定到ng repeat
角码^Angularjs Angular$scope.model无法绑定到ng repeat,angularjs,scope,ng-repeat,Angularjs,Scope,Ng Repeat,角码^ angular.module('demoapp').controller("EventControllerNearPosition", EventControllerNearPosition); EventControllerNearPosition.$inject = ["$scope", "ApiEventFactory", "ApiPositionFactory", "UserInteractionMessagesFactory", "$location"] function E
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("/");
});
};
};
安格位置
{{position.location{u name}}
select不会绑定我从i api获得的位置。我已经调试了应用程序,并检查了是否返回了正确的值。我知道类似或“相等”的代码在其他局部视图中工作,所以我不明白为什么它在这里不工作。而且,当我按下按钮时,它将触发“SearchEvent”,调试器中不会发生任何事情。如果有必要的话,我会使用firefox
谢谢你的反馈 我只能看到一个错误,您应该从
的响应对象中获取数据
。然后
函数
<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>
如果我能看到你得到的数据,我就能做出很好的判断。但就这段代码而言,
.then()
并没有得到很好的使用。.then方法的作用是.then(响应函数、错误函数)
不需要使用.error()
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("/");
});
};
我已经解决了我的问题。首先,我删除了“s”作为回应。请求的位置 然后我使用了提交的answear Pankaj Parkar。除了我没有在response.data.requested_位置中使用“data”,因为我使用“$resource”,而不是“$http”或其他任何名称
感谢所有的反馈 响应中没有“数据”。这是一个很好的回答。Requested_positions是一个包含position对象的数组。我得到的“response”变量包含一个对象的“Requested_position”变量数组。根据惯例,您的控制器将调用工厂方法。工厂方法将生成$http请求。这将在.then()中等待响应,并以$q作为承诺。如果请求得到响应,它将执行$q.resolve(),否则将执行$q.reject。在这里,如果您想访问从工厂收到的控制器中的数据,它将始终有一个
data
元素,您将在其中找到您收到的数据,其他变量将保存与您的输出不直接相关的信息。
function getAllPositions() {
apiPosition.getAllPositions().then(function(response){
$scope.positions = response.data.requested_positions;
},function(error){
UIMfactory.addUserFailedMessage("Something went wrong, try again!");
$location.path("/");
});
};