<div ng-app="demo">
<div ng-controller="DefaultController as ctrl" ng-init="ctrl.setProject(8)">
<select ng-options="project.ProjectID as project.ProjectID for project in ctrl.projects" ng-model="ctrl.projectID">
</select>
</div>
</div>
angular
.module('demo', [])
.controller('DefaultController', DefaultController);
function DefaultController() {
var vm = this;
vm.projects = 'Your data goes here!';
vm.setProject = setProject;
function setProject(projectID) {
vm.projectID = projectID;
}
}
I have shared a jsfiddle link here, where the ng-options is being used to generate <option>
tags within a <select>
element, making it dynamic and interactive.
The usage of the "as" clause in the ng-options is crucial as it helps in setting the selected option based on the specified property (in this case, projectID), ensuring smooth functionality.
While the ng-init directive has been utilized in the provided example to initialize the selected option using a specific function, it's recommended to handle such logic directly within the controller for better code organization.
If you are interested in enhancing your Angular skills, I suggest taking a look at John Papa's style guide, which offers valuable insights and best practices.