1

Hey I have problem with set default value in select problem is started when I set inside array objects. How can I fix this problem??

HTML

<div ng-app="select" ng-controller="checking">
  <select ng-model="lala" ng-options="item.name for item in arrayName">
  </select>
</div>

JavaScript

var app = angular.module('select', []);

app.controller('checking', ($scope)=> {
  $scope.arrayName = [{'name':'first'}, {'name':'second'}, {'name':'third'}]; 
  $scope.lala = "first";
});

https://codepen.io/Turqus/pen/EbvGzv?editors=1111

1
  • Please include the code in the question next time, links to code are fine to support the question but if that link were to die this question would lose all context Commented Nov 15, 2017 at 11:53

5 Answers 5

1

All you need to do is change your ng-options to include a select as

<select ng-model="lala" ng-options="item.name as item.name for item in arrayName">

var app = angular.module('select', []);

app.controller('checking', function($scope) {
  $scope.arrayName = [{
    'name': 'first'
  }, {
    'name': 'second'
  }, {
    'name': 'third'
  }];

  $scope.lala = 'first';

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="select" ng-controller="checking">

  <select ng-model="lala" ng-options="item.name as item.name for item in arrayName"></select>
  
  <p>Value of lala: <b>{{lala}}</b></p>
  
</div>

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

Comments

1

You can try below example you have to change array bit

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="prop.value" ng-options="v for v in prop.values">
    </select>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) 
{
    $scope.prop = 
    {
        "type": "select", 
        "name": "Service",
        "value": "first", 
        "values": ['first','second','third'] 
    };
});
</script>

5 Comments

so, if I want have default value in select, I can have only if parameter object is equall parameter object or variable equall variable yes?
if I want default value Its can be only when I have parameter in object and this second value is in object too yes?
You can use like "value": "" if you dont want to select default value
can u give me example??
Like this $scope.prop = { "type": "select", "name": "Service", "value": "", "values": ['first','second','third'] };
0

Well you can achieve it by using ng-init as below

<div ng-app="select" ng-controller="checking">

  <select ng-init="lala = arrayName[0]" ng-model="lala" ng-options="item.name for item in arrayName"  >


  </select>
</div>

<script>
    var app = angular.module('select', []);

    app.controller('checking', ($scope)=> {
      $scope.arrayName = [{'name':'first'}, {'name':'second'}, {'name':'third'}]; 
      $scope.lala = "first";
    });
<script>

here is the codepen example and hope it will help.

2 Comments

Using ng-init for this is not recommended by the documentation and is considered bad practice (even though it works!) docs.angularjs.org/api/ng/directive/ngInit
You don't even need ng-init here, you could just do $scope.lala = $scope.arrayName[0] in the controller
0

I think you just need to assign the ng-model like this.

 $scope.lala = $scope.arrayName[0];

Here is your codepen link

Comments

0
var app = angular.module('select', []);

app.controller('checking', ($scope)=> {
  $scope.arrayName = [{'name':'first'}, {'name':'second'}, {'name':'third'}]; 
  $scope.lala = $scope.arrayName[0].name;
});

2 Comments

My recommendation trying this way.
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.