javascript - choose function by select in angularjs -
i have 2 selects , 1 button function. want check if location selected location part of code, if assigned selected assigned part of code. works if both null in api ;), can change 1 of them.
$scope.edit_location = function() { for(var = 0; < $scope.inventories.length; i++) { if($scope.currentinventory.location){ console.log('i 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 in assigned'); var copyselectedinv2 = restangular.copy($scope.currentinventory); copyselectedinv2.customput({location: null, assigned: $scope.currentinventory.assigned, tags: $scope.inventories[i].tags}); } } };
my template selects
<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 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()">
i use ng-options
instead:
<select class="input-medium form-control" ng-model="currentinventory.location" ng-options="location.resource_uri location.name location in locations"> <option value="">choose location</option> </select>
if need selecting value deselect other, quick solution is:
$scope.$watch("currentinventory.assigned",function(newvalue,oldvalue,scope){ scope.currentinventory.location = null; }); $scope.$watch("currentinventory.location",function(newvalue,oldvalue,scope){ scope.currentinventory.assigned = null; });
another solution binding object
value instead of resource_uri
property:
<select class="input-medium form-control" ng-model="currentinventory" ng-options="location.name location in locations"> <option value="">choose location</option> </select>
and check whether it's location or user:
$scope.edit_location = function() { for(var = 0; < $scope.inventories.length; i++) { if($scope.locations.indexof($scope.currentinventory)>=0){ console.log('i 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 in assigned'); var copyselectedinv2 = restangular.copy($scope.currentinventory); copyselectedinv2.customput({location: null, assigned: $scope.currentinventory.assigned, tags: $scope.inventories[i].tags}); } } };
Comments
Post a Comment