0

I am new to angular. I have the select declared as such (jade syntax)

select(ng-model="projectData.ProjectStatus" ng-options="c.name for c in statuses")

and statuses are initialised as

$scope.statuses = [
  {name:'Active', value:1},
  {name:'Not active', value: 0}
];

In resulting select, nothing is selected and it is rendered as

<select
  ng-model="projectData.ProjectStatus"
  ng-options="c.name for c in statuses"
  class="ng-pristine ng-valid">
    <option value="?" selected="selected"></option>
    <option value="0">Active</option>
    <option value="1">Not active</option>
</select>

How I can specify which option I need to have selected, and how to avoid rendering of first option with "?" so I simply have a select where "Not active" or "active" is already chosen?

Note that $scope.projectData.ProjectStatus IS sent to 1 but it does not seem to be in effect here...

1
  • Maybe I think create currentSelection variable on $scope and use it. Commented Feb 17, 2014 at 14:07

2 Answers 2

1

You should not manually use options tag instead try the below code to make it working

<body ng-controller="MainCtrl" >
 <div>
   <select ng-model="projectData.ProjectStatus" ng-options="c.value as c.name for c in data"></select>
</div>
    <script>
        var app = angular.module('demo', []);
        alert(app);

        app.controller('MainCtrl', function ($scope) {
            $scope.data = [{ name: 'Active', value: 1 }, { name: 'Not active', value: 0 }];
            $scope.projectData = { ProjectStatus: 0 }
        });

    </script>
</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You can set default value for select by setting model value in your controller:

$scope.projectData.ProjectStatus = $scope.statuses[0];

Also, note that option values rendered in html are actually indexes of array you pass in ng-options. More info in API Doc.

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.