简体   繁体   English

仅使用一个选项重置Angular JS中的选择框

[英]Resetting a select box in Angular JS with a only one option

I don't know if what I'm experiencing is a bug, but I can't seem to reset a select box in Angular JS 1.0.2 (also tested with 1.1.5) where there is only one option. 我不知道我遇到的是一个错误,但我似乎无法重置Angular JS 1.0.2中的选择框(也用1.1.5测试),其中只有一个选项。 This is for a iPad app wrapped in Phonegap. 这是一个包含在Phonegap中的iPad应用程序。 I've tested in the browser (Safari, Chrome) though and the issue is still there. 我已经在浏览器(Safari,Chrome)中进行了测试,问题仍然存在。

I'm working on an app that has many products that are in different categories and sub-categories. 我正在开发一款应用程序,其中包含许多不同类别和子类别的产品。 When you select a category the route changes and normally resets the select box. 当您选择类别时,路线会更改并通常会重置选择框。 And it looks like so: 它看起来像这样:

在此输入图像描述

However, if you were to choose an option and then decide to choose another sub-category when there is only one option in the select box for a sub-category (when the user clicks one of the images where it says "Other Products") the select box doesn't properly reset. 但是,如果您要选择一个选项,然后在子类别的选择框中只有一个选项时(当用户单击其中一个图像显示“其他产品”时),则决定选择另一个子类别选择框未正确重置。 The userthen can't advance from this point to the next select box. 使用时无法从此点前进到下一个选择框。 It looks like this: 它看起来像这样:

在此输入图像描述

I've almost gotten it to work by coming up with this function from the various resources out there, but seems Angular is being quirky. 通过从各种资源中提出这个功能,我几乎得到了它的工作,但似乎Angular是古怪的。 It looks like this with where I've got so far: 到目前为止我看起来像这样:

在此输入图像描述

The problem is that I want the blank space to be before the option, not after. 问题是我希望空白区域位于选项之前,而不是之后。 Then the user has to click the second blank option and then click the option again in order to activate the second select box. 然后,用户必须单击第二个空白选项,然后再次单击该选项以激活第二个选择框。

Here is the JS: 这是JS:

 $scope.reset = function() {
    $scope.variants.selectedIndex = 0;
};

Here is the JSON. 这是JSON。 Notice that these set of variants have the same size: 请注意,这些变体集具有相同的大小:

1: {
      name: 'Super Awesome Product',
      description: 'Cool description',
      category: 'viewers',

      variants: {
    1: {
         color: 'Gold',
         size: '55-62mm',
         productCode: 'FCSTVG',
         price: 0,
         image: [path + 'FCSTVG-T.png', path + 'FCSTVG.png']
       },
    2: {
         color: 'Silver',
         size: '55-62mm',
         productCode: 'FCSTVS',
         price: 0,
         image: [path + 'FCSTVS-t.png', path + 'FCSTVS.png']
       }
     }
   }
 };

And the HTML for the select box: 选择框的HTML:

<select ng-model="selectedVariant" ng-show="variants != null">
    <option ng-repeat="size in variants" value="{{size}}">
        {{size[0].size}}
</option>
</select>

And the HTML for the where my reset() is clicked. 以及单击我的reset()的HTML的HTML。 Like I had said, these are the "Other Products" of images below: 就像我说的那样,这些是以下图像的“其他产品”:

<div class="other-products">
   <h2>Other products</h2>
      <div class="slide-container">
        <ul ng-show="products != null" style="width: {{products.length * 112}}px">
       <li ng-repeat="p in products" ng-show="findDefaultImage(p) != null">
          <a href="#" eat-click ng-click="selectProduct(p.id);reset()">
         <img ng-src="{{findDefaultImage(p)}}" alt="" />
          </a>
       </li>
     </ul>
       </div>
 </div>

I've tried everything, like adding different values to this line $scope.variants.selectedIndex = 0; 我已尝试过所有内容,例如为此行添加不同的值$scope.variants.selectedIndex = 0; like -1 or 1. 像-1或1。

Any help would be appreciated! 任何帮助,将不胜感激!

UPDATE: I solved the issue by hardcoding it. 更新:我通过硬编码解决了这个问题。 Didn't know why I didn't do it before, but I'm still endorsing @Shawn Balestracci answer as it answers the question. 不知道为什么我之前没有这样做,但我仍然支持@Shawn Balestracci答案,因为它回答了这个问题。

Angular has a tendency to empty out the "index 0" of a select box pushing all of the options back 1 in the select box, but in actuality the option that a user selects is actually the next option in the drop down list. Angular倾向于清空选择框中的“索引0”,将所有选项推回到选择框中的1,但实际上用户选择的选项实际上是下拉列表中的下一个选项。 I don't know if this is a bug or a feature. 我不知道这是一个错误还是一个功能。

Here's how I hardcoded the HTML: 这是我如何硬编码HTML:

<select ng-model="selectedVariant" required="required" ng-show="variants != null">
    <option style="display:none" value="">PICK ONE:</option>
    <option ng-repeat="size in variants" value="{{size}}">
        {{size[0].size}}
</option>
</select>

This stops Angular from pushing back the options in the drop down. 这会阻止Angular推回下拉列表中的选项。

In your reset function just change $scope.selectedVariant to a value that isn't in the list. 在重置函数中,只需将$scope.selectedVariant更改$scope.selectedVariant中的值。 It's the value stored in that model that will determine which option is selected (and vice-versa.) 它是存储在该模型中的值,用于确定选择哪个选项(反之亦然)。

You should also be able to take advantage of the ngOptions directive isntead of creating them via ng-repeat. 您还应该能够利用ngOptions指令而不是通过ng-repeat创建它们。 http://docs.angularjs.org/api/ng.directive:select http://docs.angularjs.org/api/ng.directive:select

 $scope.reset = function() {
    $scope.selectedVariant = {};
};

So if there is only one option, why use a select box? 因此,如果只有一个选项,为什么要使用选择框? Or should the second option be "none". 或者第二个选项应该是“无”。

In anycase, if you could put together a simple jsfiddle, that would help folks better see the problem and be able to play with it and hopefully find you an answer. 在任何情况下,如果你可以把一个简单的jsfiddle放在一起,这将有助于人们更好地看到问题,并能够使用它,并希望找到答案。

I disagree with resetting the select model to an empty object. 我不同意将选择模型重置为空对象。 That will blank out the list of options you may have there. 这将删除您可能在那里的选项列表。 Instead you can set the selected option to zero using the model: 相反,您可以使用模型将所选选项设置为零:

$scope.reset = function() {
    $scope.selectedVariant = 0;
};

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

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