Javascript ng重复显示第一个选项的默认空值
ng repeat显示第一个选项的默认空字符串。为什么在五月的情况下它不起作用。这里怎么了 我的角度代码在这里。我错在这里了。我不明白Javascript ng重复显示第一个选项的默认空值,javascript,html,angularjs,Javascript,Html,Angularjs,ng repeat显示第一个选项的默认空字符串。为什么在五月的情况下它不起作用。这里怎么了 我的角度代码在这里。我错在这里了。我不明白 var exchange = angular.module('app', []); exchange.controller('ExchangeController', ExchangeController); function ExchangeController($scope, $http) { $http .get(window
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);
}
}
这是我的html代码
<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>
@{{item.text}
您需要匹配选项值和ng model
值,以防止出现默认空白。在您的情况下,它不匹配,因为选项值前面有@
符号。移除它
<option ng-repeat="item in items" value="{{ item.value }}">@{{ item.text }}</option>
@{{item.text}
您需要手动选择一个“选定”项(itemSelected
我的解决方案中的模型),并将值属性更改为ng值
,使其以AngularJS方式工作。请检查我为您创建的。顺便说一句,这里不需要jQuery:请检查我如何在getSendItem()
中记录所选项目。这适用于AngularJS 1.6.x
或更高版本
看法
在使用AngularJS 1.5.x或更早版本时,您需要像这样使用ng selected
:
看法
试试这个。这对我有用
<option value="{{item.value}}" ng-repeat="item in items" ng-selected="$first">{{ item.text }}</option>
{{item.text}
请添加响应.数据.项目的结果数据
。感谢这样的响应结果:[{“text”:“Bkash”,“value”:1},{“text”:“Paypal”,“value”:2}]谢谢,我们能找到这个数据插件selectTwo
指令吗?我不确定。我正在使用一个在这里构建的模板。谢谢…我做了和你完全一样的事。但在可能的情况下,它不起作用。@Mahfuzhishishir请仔细比较你的解决方案和我的代码,你会没事的。我理解背后的逻辑。我不明白为什么在五月份它不起作用。从函数中记录所选项可以正常工作,但当我想为第一个元素选择时,它不工作。$http.get(window.location.origin+“/api/get item/”,{transformRequest:angular.identity,headers:{'Content-Type':undefined,'Process Data':false})。然后(函数(响应){$scope.items=response.data.items;$scope.itemSelected=$scope.items[0].value;});$scope.getSendItem=function(){console.log($scope.itemSelected);}@mahfushir您能创建一个小提琴来重现您的问题吗?
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);
}
});
<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>
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);
}
<option value="{{item.value}}" ng-repeat="item in items" ng-selected="$first">{{ item.text }}</option>