在angularjs中选择值时,html中的选择标记值会消失
16lz
2021-01-22
I have a select tag in html which has a value from angularjs. Everytime I select a value, the select tag will be empty. Why? How to fix it?
我在html中有一个select标签,它有一个来自angularjs的值。每次我选择一个值时,select标签都是空的。为什么?怎么解决?
$scope.getAll = function(){
$http.post()....
$scope.places = response.data;
};
$scope.getAll();
$scope.sample = function(str){
alert(str);
};
HTML
HTML
<select ng-model="val" ng-change="sample(val)">
<option ng-repeat="place in places" value="place.name">{{place.name}}</option>
</select>
In code above, when I select some value of the select tag it alerts me the value but the select tag goes empty!
在上面的代码中,当我选择select标签的某个值时,它会提醒我值,但select标签变为空!
2 个解决方案
#1
2
i recommend instead of using ng-repeat
use ng-options
我建议使用ng-repeat而不是使用ng-repeat
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>
更多相关文章
- HTML5绘图之Canvas标签 绘制坐标轴
- 如何在execCommand formatBlock 'p'标签中添加类或id或CSS样式?
- script标签写在哪里好?
- 从动态生成的音频标签中收听audio.ended
- 点击a标签,返回上一页
- JS实现60s倒计时(亲测有效),及span标签如何使用和禁用onclick事件
- 等待执行所有ajax回调的最佳解决方案
- FusionCharts的Line.swf做法,我想出现2条图形线,怎么设置graph标签
- 如何在chart.js中为饼图添加标签