简体   繁体   English

使用AngularJS在select中创建的带有空字符串的空白选项

[英]Blank option with empty string created in select using AngularJS

I've created an angularJS select box which will filter the results in a table based on the selected value in the select box. 我创建了一个angularJS选择框,该框将根据选择框中的所选值过滤表中的结果。

Now, the select box is created using an object 'user['location']' which has locations as keys. 现在,使用具有位置作为键的对象'us​​er ['location']'创建选择框。 Also, I'm grabbing the default user location '${city}' as soon as the page is loaded, passing it on to my select box, and filter the results accordingly in the table. 此外,我将在页面加载后立即获取默认用户位置'$ {city}',并将其传递到我的选择框中,并在表中相应地过滤结果。

If the user's current location doesn't match any of the options in my select box, then no filter should be applied! 如果用户的当前位置与我的选择框中的任何选项都不匹配,则不应应用任何过滤器!

For eg, if the user location is 'London', since there's nothing like in 'London' in my object, it should select the first option - 'Select City'. 例如,如果用户位置为“伦敦”,则由于我的对象中的“伦敦”没有类似的内容,因此应选择第一个选项-“选择城市”。

But currently it is creating an empty string like <option value= "? string:London ?"></option> above that and is selecting it! 但是目前它正在上面创建一个空字符串,如<option value= "? string:London ?"></option>并选择它!

How, do fix it? 如何解决?

Here's my code: 这是我的代码:

HTML: HTML:

<select class="form-control" ng-model="user.loc" ng-init="user.loc = '${city}'">
  <option value="" ng-selected="!checkKey(user.loc)">Select City</option>            
  <option value="{{key}}" ng-selected="key == user.loc" ng-repeat="(key, value) in user['location']">{{key}}</option> 
</select>

JS: JS:

$scope.user['location'] = {Sydney: 5, Hong Kong : 7, NYC : 3, Toronto: 1};

$scope.checkKey = function(loc) {
  if(loc in $scope.user['location']){
    return true;
  } else {
    return false;
  }            
};

I think I understand what you are trying to do here. 我想我知道您在这里想做什么。 But instead of checking the values using checkKey , you can do it once when your controller is loaded. 但是,不必使用checkKey来检查值,而是可以在加载控制器时执行一次。

Also, you can leverage ngOptions to render available options in the select box. 另外,您可以利用ngOptions在选择框中呈现可用选项。

 angular.module('myapp', []) .controller('myctrl', function($scope) { $scope.user = {}; $scope.user['location'] = { 'Sydney': 5, 'Hong Kong': 7, 'NYC': 3, 'Toronto': 1 }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp" ng-controller="myctrl"> <select class="form-control" ng-model="user.loc" ng-init="user.loc = user.location['London']" ng-options="value as key for (key, value) in user.location"> <option value="">Select City</option> </select> </div> 

You can change ng-init with your own value, as you were doing, and it should work fine with it. 您可以像以前一样用自己的值更改ng-init ,它应该可以正常工作。

Ok, I tried this and it worked! 好的,我尝试了一下,它成功了!

<select class="form-control" ng-model="user.loc">
  <option value="" ng-selected="!checkKey(user.loc)">Select City</option>            
  <option value="{{key}}" ng-selected="key == '${city}'" ng-repeat="(key, value) in user['location']">{{key}}</option> 
</select>

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

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