My purpose is to make a drop down select with options from 0 to 9 digits. I'm actually composing 4 such modules together and running calculations. Now I met problem on reset the 4 selects into a default state after user accidentally selected options that can not be posted to the backend.
<select ng-init="q8_1_selected" ng-model="q8_1_selected" ng-change="q8_select_digit_1(q8_1_selected)" ng-options="item1 for item1 in q8_digit_array"> {{item1}} </select> I have this simple select code using Angular, now I can get the q8_1_selected value with $scope.q8_1_selected no problem, however when I set the value like
$scope.q8_1_selected = 1; This is not updating the selected option on the web page. What's wrong?
q8_digit_array is [0..9], a digit array.
Just tried,
<option ng-selected="item1 == q8_1_selected">{{item1}}</option> to replace {{item1}}, but after $scope.q8_1_selected = 1; it still not changing anything on the web page. After removing ng-init, still not working.
Update: It is just the web page not updating.
Maybe I should show the whole picture, I'm actually using Ionic, which uses AngularJS for all the bindings.
<select class="q8_select" ng-model="q8_1_selected" ng-change="q8_select_digit_1(q8_1_selected)" ng-options="item1 for item1 in q8_digit_array"> {{item1}} </select> js,
//change function, I have total 4 like this one. $scope.q8_select_digit_1 = function(digit){ $scope.q8_1_selected = parseInt(digit) ; q8_update_answer() ; } //each time the answer must be calcluated var q8_update_answer = function(){ var n1 = $scope.q8_1_selected * 10 + $scope.q8_2_selected ; var n2 = $scope.q8_3_selected * 10 + $scope.q8_4_selected ; //!!!!---- Problem happens here, the following changes on $scope.q8_1_selected etc. Sometimes working, but most of the time, not updating the web page ----!!!! //However the value of $scope.q8_1_selected and all others are correct. The data is binded, at the JS side. if (n1 == 0){ $scope.q8_1_selected = 0 ; $scope.q8_2_selected = 1 ; } else if (n2 == 0){ $scope.q8_3_selected = $scope.q8_1_selected ; $scope.q8_4_selected = $scope.q8_2_selected ; } else if (n2 < n1){ $scope.q8_3_selected = $scope.q8_1_selected ; $scope.q8_4_selected = $scope.q8_2_selected ; } n1 = $scope.q8_1_selected * 10 + $scope.q8_2_selected ; n2 = $scope.q8_3_selected * 10 + $scope.q8_4_selected ; $rootScope.answerData[$scope.whichQuestion-1] = [n1,n2] ; }
q8_digit_array? Try to set one of the array value in$scope.q8_1_selected = array_valueinstead$scope.q8_1_selected = 1;$scope.q8_1_selected = true;ng-init="q8_1_selected"? Please try without this.