gpt4 book ai didi

javascript - 在 angularjs 中选择值时,html 中的选择标签值消失

转载 作者:行者123 更新时间:2023-11-28 14:55:34 26 4
gpt4 key购买 nike

我在 html 中有一个 select 标签,它具有来自 angularjs 的值。每次我选择一个值时,选择标签都会为空。为什么?如何解决?

$scope.getAll = function(){
$http.post()....
$scope.places = response.data;
};

$scope.getAll();

$scope.sample = function(str){
alert(str);
};

HTML

<select ng-model="val" ng-change="sample(val)">
<option ng-repeat="place in places" value="place.name">{{place.name}}</option>
</select>

在上面的代码中,当我选择选择标记的某些值时,它会提醒我该值,但选择标记变为空!

最佳答案

我建议不要使用ng-repeat,而是使用ng-options

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.places = [ {name: 'California'},
{name: 'New York'},
{name: 'Cochin'}
];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="val" ng-change="sample(val)" ng-options="place.name as place.name for place in places">
</select>
{{val}}
</div>

关于javascript - 在 angularjs 中选择值时,html 中的选择标签值消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com