When working with AngularJS, it's important to utilize the ng-model attribute in your HTML code. This attribute allows you to bind the value to a variable from the controller:
For example, in your HTML code:
<div ng-controller="ExampleController">
<form name="myForm" ng-submit="submit()">
<label for="repeatSelect"> Repeat select: </label>
<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
<option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
<hr>
<tt>repeatSelect = {{data.repeatSelect}}</tt><br/>
</div>
In the corresponding controller class:
angular.module('ngrepeatSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
repeatSelect: null,
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
};
$scope.submit = function() {
// Here you can define logic to handle onClick action by pressing the Button.
var selectValue = $scope.data.repeatSelect;
// Perform operations using selectValue variable
}
}]);
If you prefer using the ng-click
method instead of a HTML form
, simply remove the form tag and update the button like so:
<input type="submit" id="submit" value="Submit" ng-click="$scope.submit()" />
Keep in mind that the ng-model immediately binds the value, allowing you to easily display it elsewhere in your application. By changing the dropdown selection, the displayed value will automatically update.
As a general recommendation, always refer to the official AngularJS documentation first when encountering issues. It can be very helpful in finding solutions to various problems.