2

I have created a service, written a code grabbing the names of the users in the AngularJS controller and am calling it in the view but i dont see anything :/ am i doing something incorrectly? new to angularJS btw.

this is the revised angular controller

6
  • Is there a reason you are not using ng-options? Commented Dec 21, 2015 at 16:19
  • Read this: stackoverflow.com/questions/12139152/… you should use ng-options for the select box rather than ng-repeat. Commented Dec 21, 2015 at 16:20
  • Are you seeing any error? If yes, please include that Commented Dec 21, 2015 at 16:20
  • you can't have the NG-MODEL and NG-OPTIONS use the same object ($scope.allUsers) , that would be partly why your code isn't working. Whenever you make a selection in the dropdown, it would overwrite your allUsers model. Commented Dec 21, 2015 at 18:08
  • any chance you can put together a plunker or jsfiddle? Commented Dec 21, 2015 at 18:12

2 Answers 2

1

Official way of generating options in AngularJS:

<select ng-model="mySelection" ng-options="user.value as user.userName for user in allUsers">
    <option value=""></option>
</select>

Also, you are setting ng-model to be the same as the thing being looped, hence you're seeing nothing.

ng-model will be the variable which will save your selection from allUsers.

Sign up to request clarification or add additional context in comments.

2 Comments

so ng-model should be equal to "users" ? its the variable in my controller if i am understanding this correctly
don't use the same variable in ng-options and ng-model, the ng-options should use allUsers while ng-model should have a different variable, like selectedUser
1

You need to use ng-options directive to generate <option> elements. ng-model directive is used to specify model property to store selected option.

Example from official documentation:

<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select>

You can read more here: https://docs.angularjs.org/api/ng/directive/ngOptions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.