0

this is my angular

angular.module("myApp",[])
.controller("myCtrl",function($scope){
  $scope.projectObj = {"proId":"33","proName":"samal"}
  $scope.projectArr = [{"proName":"samal","proId":"33"},{"proName":"aaa","proId":"47"},{"proName":"sdf","proId":"48"},{"proName":"sdf","proId":"49"},{"proName":"sddd","proId":"50"},{"proName":"dddd","proId":"51"},{"proName":"ttt","proId":"52"},{"proName":"sdf","proId":"53"},{"proName":"sdf","proId":"54"},{"proName":"sdf","proId":"55"},{"proName":"sdf","proId":"56"},{"proName":"sdf","proId":"57"},{"proName":"sdf","proId":"58"},{"proName":"sdf","proId":"59"},{"proName":"sdf","proId":"60"}]
})

this is my select box

<select ng-model="projectObj">
   <option value="{{item}}" ng-repeat="item in projectArr">{{item.proName}}</option>
</select>

$scope.projectObj is already assign with an object and it is already inside $scope.projectArr. what is want is when i load the html file $scope.projectObj proName should be pre-selected.how can i do this. this is my pluker

1
  • Check my answer it is working. Commented Jul 29, 2016 at 9:29

2 Answers 2

2

Repeating options isn't preferred way to show select dropdown. I'd suggest you to to use ng-options directive to get object binding on ng-model

<select  flex ng-model="projectObj" 
  ng-options="item as item.proName for item in projectArr">
</select> 

And then assign projectObj from projectArr.

$scope.projectObj  = $scope.projectArr[0];

Demo here


Use md-select for Angular material like below and use ng-model-options="{trackBy: '$value.proId' for directly pre select object. Additionally you have to follow Dot Rule while defining ng-model

Markup

<md-input-container>
  <md-select ng-model="projectObj" placeholder="Select a state"
    ng-model-options="{trackBy: '$value.proId'}">
    <md-option ng-value="item" ng-repeat="item in projectArr">
      {{ item.proName }}
    </md-option>
  </md-select>
</md-input-container>

CodePen

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

8 Comments

thing is i'm using angular material design for my app. when i'm using ng option its not supported in material
@IT13122256RanawakaR.A.S.M look at the updated answer please
i want to remove this $scope.projectObj = $scope.projectArr[0]; and only use like this $scope.projectObj = {"proId":"33","proName":"samal"}. then its not working y is that. both are same to in my knowledge
@IT13122256RanawakaR.A.S.M no, $scope.projectObj = $scope.projectArr[0]; is taking object from same array reference, where as the $scope.projectObj = {"proId":"33","proName":"samal"} creates a new reference
thing is i'm getting the array and object from separate request and if i use $scope.projectObj = $scope.projectArr[0] then i need to loop through projectArr array get the object and assign it to projectObj
|
0

You can use ng-selected. This will work.

<option value="{{item}}" ng-selected="item" ng-repeat="item in projectArr">{{item.proName}}</option>

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.