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-modeldirective - binds the value of the form element to a property of the scope<select>element - contains the list of itemsselectedItemproperty of the scope - contains the value of the item selected in the<select>elementconsole.log()- prints the value of theselectedItemproperty of the scope
Helpful links
More of Angularjs
- 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 Angular to zoom in on an image?
- How can I use Zone.js with Angular to detect and act upon asynchronous events?
- How can I use AngularJS with Visual Studio Code?
- How can I use Angular to zoom in and out of a div?
- How do I use Angular Zone to run my code?
- How do you use $state.go in AngularJS UI-Router?
- How do I use an AngularJS directive?
See more codes...