0

I'm trying to set a default value to the select box's options using ng-init in angular js but it isn't working. Here's the code that I have written so far.

var app = angular.module("myApp", [])

app.controller("mainController", ["$scope", function($scope){
    $scope.myoptions = "";
    $scope.mainList = [
      "firstOption",
      "secondOption",
      "thirdOption"
    ]
}]);
select{
width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<select ng-controller="mainController" ng-options="options as options for options in mainList" ng-model="myoptions" nginit="myoptions=mainList[1]">
</select>
</body> 

Please help me with it. Thanks in advance :)

1
  • Change nginit to ng-init. Commented Mar 10, 2017 at 9:54

4 Answers 4

3

you have

nginit

which should be

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

Comments

2

It will be ng-init there is no nginit directive in angular js

var app = angular.module("myApp", [])

app.controller("mainController", ["$scope", function($scope){
    $scope.myoptions = "";
    $scope.mainList = [
      "firstOption",
      "secondOption",
      "thirdOption"
    ]
}]);
select{
width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<select ng-controller="mainController" ng-options="options as options for options in mainList" ng-model="myoptions" ng-init="myoptions=mainList[1]">
</select>
</body> 

Comments

2

ng-init="myoptions=mainList[0]"

Comments

1

change nginit="myoptions = mainList[1]"

to ng-init="myoptions = mainList[1]"

var app = angular.module("myApp", [])

app.controller("mainController", ["$scope", function($scope){
    
    $scope.mainList = [
      "firstOption",
      "secondOption",
      "thirdOption"
    ]
     
}]);
select{
width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<select ng-controller="mainController" ng-options="options as options for options in mainList" ng-model="myoptions" ng-init="myoptions = mainList[1]">
</select>
</body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.