javascript - ng-repeat show default empty value for first option -
the ng-repeat show default empty string first option. why in may case not work. wrong in here?
my angular code here. mistake here. don't understand.
var exchange = angular.module('app', []); exchange.controller('exchangecontroller', exchangecontroller); function exchangecontroller($scope, $http) { $http .get(window.location.origin + "/api/get-item/", { transformrequest: angular.identity, headers: {'content-type': undefined, 'process-data': false} }) .then(function(response){ $scope.items = response.data.items; $scope.send_item_id = $scope.items[0].value; }); $scope.getsenditem = function() { var send_item_id = $("#send_item_id").val(); console.log(send_item_id); } }
here html code,
<div class="form-group"> <div class="col-md-12 col-sm-12"> <select data-plugin-selecttwo name="send_item_id" id="send_item_id" ng-model="send_item_id" ng-change="getsenditem()" class="form-control populate"> <option ng-repeat="item in items" value="@{{ item.value }}">@{{ item.text }}</option> </select> </div> </div>
you need manually select "selected" item (itemselected
model in solution) , change value
attribute ng-value
make work in angularjs way. please check demo fiddle i've created you. btw. don't need jquery here: please check how log selected item in getsenditem()
. work angularjs 1.6.x
or later.
view
<div ng-controller="myctrl"> <select data-plugin-selecttwo name="send_item_id" id="send_item_id" ng-model="itemselected" ng-change="getsenditem()" class="form-control populate"> <option ng-repeat="item in data" ng-value="item.value"> @{{ item.text }} </option> </select> </div>
angularjs application
var myapp = angular.module('myapp', []); myapp.controller('myctrl', function($scope) { $scope.data = [{ "text": "bkash", "value": 1 }, { "text": "paypal", "value": 2 }]; $scope.itemselected = $scope.data[0].value; $scope.getsenditem = function() { console.log($scope.itemselected); } });
while using angularjs 1.5.x or earlier need in demo fiddle using ng-selected
:
view
<div ng-controller="controller"> <select name="send_item_id" id="send_item_id" ng-model="itemselected" ng-change="getsenditem()"> <option ng-repeat="item in data" value="{{ item.value }}" ng-selected="{{item.value === itemselected}}"> {{ item.text }} </option> </select> </div>
angularjs application
var myapp = angular.module('app', []); myapp.controller('controller', function($scope) { $scope.data = [{ "text": "bkash", "value": 1 }, { "text": "paypal", "value": 2 }]; $scope.itemselected = string($scope.data[0].value); console.log($scope.itemselected); $scope.getsenditem = function() { console.log($scope.itemselected); }
Comments
Post a Comment