i'm trying preselected value within , i've tried multiple tutorials , looked answers here none worked. here's deal ->
load shifts $http.get("api/shifts"), then:
<select multiple class="form-control" ng-options="shift shift.nom shift in shifts" ng-model="selectedshift"></select> and goes modal window. before modal window opens, can preselect shift (to see able it). in case, have
if(preselectedshift){ $http.get("api/shifts/"+preselectedshift._id).success(function(shift){ $scope.selectedshift = shift; //so it's replacing ng-model in <select> }) } and shifts appear should, gives me no preselected shift. tried loop.
for (shifts.length) -> if(shift[i]._id == preselectedshift._id) ->$scope.selectedshift = shift[i] which gave me error "value.foreach not function"
nor ng-select="shift._id == preselectedshift._id" has worked (gives same error).
thank in advance!
i think you're pretty close solution, it's foreach not supported natively, if use loop should good:
$scope.selectedshift = []; if(preselectedshift){ $scope.selectedshift.length = 0; $http.get("api/shifts/"+preselectedshift._id).success(function(shift){ for(var x = 0; x < $scope.shifts.length; x++){ if($scope.shifts[x]['_id'] === shift['_id']){ $scope.selectedshift.push($scope.shifts[x]); } } }) } the reason have because in ng-options use shift shift.nom ... means in order selected has same reference array not equaling it. here's quick example explaining (hopefully) how angular checks see if select something:
var mytest = { test: 4}; var myarray = [{ test: 4 }]; var mytest2 = myarray[0]; console.log(myarray[0] === mytest); //this outputs false console.log(myarray[0] === mytest2); //this outputs true
Comments
Post a Comment