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 use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
See more codes...