简体   繁体   English

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

[英]Select tag value in html disappears when selecting a value in angularjs

I have a select tag in html which has a value from angularjs. 我在html中有一个select标记,该标记具有angularjs的值。 Everytime I select a value, the select tag will be empty. 每当我选择一个值时,select标记将为空。 Why? 为什么? How to fix it? 如何解决?

$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标签为空!

When setting the value of the option as an expression, try using ng-value . 将选项的值设置为表达式时,请尝试使用ng-value While your question is confusing, I'm guessing that the alert shows up as place.name 当您的问题令人困惑时,我猜测该警报显示为place.name

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

Or if you want to set the value of the select attribute itself, you can use, 或者,如果您想设置select属性本身的值,则可以使用,

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

https://plnkr.co/edit/fAbqV1wG2VuF069lt3bm?p=preview https://plnkr.co/edit/fAbqV1wG2VuF069lt3bm?p=preview

This might be able to guide you further. 这也许可以进一步指导您。

i recommend instead of using ng-repeat use ng-options 我建议不要使用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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM