Javascript angular不从复杂对象中选择selectbox选项
当对象来自服务器时,我无法选择选项Javascript angular不从复杂对象中选择selectbox选项,javascript,angularjs,Javascript,Angularjs,当对象来自服务器时,我无法选择选项 <select class="form-control" ng-hide="trip.checked1" ng-model="trip.location" ng-change="tripLocationChange(shift, trip)" ng-options="obj as obj.text for obj in locations" requi
<select class="form-control"
ng-hide="trip.checked1"
ng-model="trip.location"
ng-change="tripLocationChange(shift, trip)"
ng-options="obj as obj.text for obj in locations" required>
</select>
我在selectBox中填充了
$scope.locations = [{"text":"Bar","value":"f07a2bc4"},{"text":"Foo","value":"f6a62517"}]
我相信问题就在这里
ng options=obj作为obj。位置中obj的文本
任何想法都值得欣赏问题在于,即使来自服务器的对象(您将其设置为$scope.trip.location)与$scope.locations数组中的对象相似,但它们是不同的对象。同时,角度检查对象是否相等,以便将selectbox选项设置为selected,并且只有当两个对象是同一对象时,两个对象才相等。这不是你的情况 在这种情况下,您必须循环遍历$scope.locations数组,找到合适的对象并将$scope.trip设置为find值。这应该适合您:
// trip object came from server
var trip = {"location":{"text":"Foo","value":"f6a62517"}};
// use it to find similar object in $scope.locations array
$scope.trip = {
location: $scope.locations.filter(function(location) {
return location.value === trip.location.value;
})[0]
};
演示如何设置$scope.trip.location?我是从更大的上下文中获取的``轮班时也会重复。但是基本上我可以得到`{trip.location.text}}`没有问题谢谢你给我指路,让它像这样工作trips[y]。location=$scope.locations.filterfunction location{return location.value==trips[y]。location.value;}[0]
// trip object came from server
var trip = {"location":{"text":"Foo","value":"f6a62517"}};
// use it to find similar object in $scope.locations array
$scope.trip = {
location: $scope.locations.filter(function(location) {
return location.value === trip.location.value;
})[0]
};