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 use Angular to zoom in and out of a div?
- How do I use the ui-sref in AngularJS?
- How do I use Angular Zone to run my code?
- How do I integrate an Angular Yandex Map into my software development project?
- How do I format a date in AngularJS?
- How can I use AngularJS to create a zone in my software development project?
- How can I become an Angular expert from a beginner level?
- How do I use Angular to zip files?
- How can I use Angular and Zorro together to create a software application?
- How can I use Zone.js with Angular to detect and act upon asynchronous events?
See more codes...