我有2个选择和一个带功能的按钮。我想检查位置是否被选中然后执行位置部分代码,如果已选择分配,则执行已分配的部分代码。如果在api中都为null,则它可以工作;),然后我可以更改其中一个。
$scope.edit_location = function() {
for(var i = 0; i < $scope.inventories.length; i++) {
if($scope.currentInventory.location){
console.log('I am in location');
var copySelectedInv = Restangular.copy($scope.currentInventory);
copySelectedInv.customPUT({location: $scope.currentInventory.location, assigned: null, tags: $scope.inventories[i].tags});
}else if ($scope.currentInventory.assigned){
console.log('I am in assigned');
var copySelectedInv2 = Restangular.copy($scope.currentInventory);
copySelectedInv2.customPUT({location: null, assigned: $scope.currentInventory.assigned, tags: $scope.inventories[i].tags});
}
}
};
我的模板选择
<select class="input-medium form-control" ng-model="currentInventory.location">
<option value="">Choose location</option>
<option value="{{location.resource_uri}}" ng-repeat="location in locations">{{location.name}}</option>
</select>
<h5>Assigne to employee</h5>
<select class="input-medium form-control" ng-model="currentInventory.assigned">
<option value="">Choose location</option>
<option value="{{user.resource_uri}}" ng-repeat="user in users">{{user.first_name + ' ' + user.last_name}}</option>
</select>
<button class="btn tbn-lg btn-success" ng-click="edit_location()">
答案 0 :(得分:0)
我会改用ng-options
:
<select class="input-medium form-control" ng-model="currentInventory.location"
ng-options="location.resource_uri as location.name for location in locations">
<option value="">Choose location</option>
</select>
如果您需要选择一个值将取消选择另一个,快速解决方案是:
$scope.$watch("currentInventory.assigned",function(newValue,oldValue,scope){
scope.currentInventory.location = null;
});
$scope.$watch("currentInventory.location",function(newValue,oldValue,scope){
scope.currentInventory.assigned = null;
});
另一种解决方案是将object
绑定为值而不仅仅是resource_uri
属性:
<select class="input-medium form-control" ng-model="currentInventory"
ng-options="location.name for location in locations">
<option value="">Choose location</option>
</select>
并检查它是位置还是用户:
$scope.edit_location = function() {
for(var i = 0; i < $scope.inventories.length; i++) {
if($scope.locations.indexOf($scope.currentInventory)>=0){
console.log('I am in location');
var copySelectedInv = Restangular.copy($scope.currentInventory);
copySelectedInv.customPUT({location: $scope.currentInventory.location, assigned: null, tags: $scope.inventories[i].tags});
}else if ($scope.users.indexOf($scope.currentInventory)>=0){
console.log('I am in assigned');
var copySelectedInv2 = Restangular.copy($scope.currentInventory);
copySelectedInv2.customPUT({location: null, assigned: $scope.currentInventory.assigned, tags: $scope.inventories[i].tags});
}
}
};