angularjsHow do I use AngularJS to select an item from a list?
To select an item from a list in AngularJS, you can use the ng-model
directive. This directive binds the value of the form element to a property of the scope. In the example below, the selectedItem
property of the scope is bound to the <select>
element.
<select ng-model="selectedItem">
<option value="item1">Item 1</option>
<option value="item2">Item 2</option>
<option value="item3">Item 3</option>
</select>
The selectedItem
property of the scope can then be accessed in the controller.
app.controller('MyCtrl', function($scope) {
console.log($scope.selectedItem);
});
The output of the example code above is the value of the item selected in the <select>
element.
Code explanation
ng-model
directive - binds the value of the form element to a property of the scope<select>
element - contains the list of itemsselectedItem
property of the scope - contains the value of the item selected in the<select>
elementconsole.log()
- prints the value of theselectedItem
property of the scope
Helpful links
More of Angularjs
- How can I become an Angular expert from a beginner level?
- How can I create an editable AngularJS application?
- How can I use the Yandex Map API with AngularJS?
- How can I use Angular to zoom in and out of a div?
- How do I install Yarn using Angular?
- How do I use AngularJS to zoom in on an image?
- How do I use Angular Zone to run my code?
- How do I use Angular to zip files?
- How do I use Angular Zone to detect and run Angular change detection?
- How do I copy an element in AngularJS?
See more codes...