简体   繁体   English

将带有ng-options的select转换为常规 <p> 与ng-repeat

[英]Translading select with ng-options to regular <p> with ng-repeat

Probably an Angular newbie question. 可能是Angular新手问题。

I have a HTML select tag to display when there is more than 1 option to provide to the user, but when there is only one option, I need to set a regular <p> , so the user may not be able to see any dropdown/select. 当有多个选项可提供给用户时,我有一个HTML select标签显示,但是当只有一个选项时,我需要设置一个常规的<p> ,因此用户可能看不到任何下拉菜单/选择。

this the select in html 这在HTML中选择

<select class="points-selection" ng-if="!noDropdown"
        ng-init="currentLine = getCurrentLine(slip)"
        ng-model="currentLine"
        ng-options="line as line.pick for line in slip.lines"
        ng-change="updateSelectionLine(slip, currentLine.pointsBought)">
</select>

with the select everything is ok 选择一切都OK

here what I have in the controller 这是我在控制器中拥有的

$scope.getCurrentLine = function(slip) {
  console.log(slip.lines.length);
  $scope.noDropdown = false;
  if (slip.lines.length === 1) {
    $scope.noDropdown = true
  };
  var lineSelected = _.find(slip.lines, function(line) {
    return line.isSelected === '1';
  });
  if (!lineSelected && slip.lines) {
    lineSelected = slip.lines[0];
  }
  return lineSelected;
};

the problem comes when I try to set that in a single p 当我尝试将其设置为单个p

  <p ng-if="noDropdown"
       ng-init="currentLine = getCurrentLine(slip)"
       ng-model="currentLine"
       ng-repeat="line in slip.lines"
       ng-change="updateSelectionLine(slip, currentLine.pointsBought)">{{currentLine}}
  </p>

so, what should I do to reproduce my select but in a single p so the user has no options to select. 因此,我应该怎么做才能重现我的select但是要用一个p来重现,这样用户就没有选择的选项。

EDIT 编辑

BTW, if I do this 顺便说一句,如果我这样做

<p ng-if="noDropdown"
   ng-init="currentLine = getCurrentLine(slip)">{{currentLine}}
</p>

or something similar, what I look printed in the DOM is this 或类似的东西,我看起来在DOM中打印的是

{"spread":"5","moneyLine":"-140","pointsBought":"0","isSelected":"0"}

I think you can simplify this. 我认为您可以简化此过程。 Just check the length of your options data with ng-if to show or hide the select box and the paragraph. 只需使用ng-if检查选项数据的长度即可显示或隐藏选择框和段落。 In your controller, if the length of the options data is 1, then set the currentLine. 在控制器中,如果选项数据的长度为1,则设置currentLine。

<select class="points-selection" ng-if="slip.lines.length!==1"
        ng-model="currentLine"
        ng-options="line as line.pick for line in slip.lines">
</select>

 <p ng-if="slip.lines.length===1">
    {{currentLine}}
  </p>

You don't need ng-model in the paragraph because you are setting that value in the controller if there is only one option. 您无需在段落中使用ng-model,因为如果只有一个选项,则可以在控制器中设置该值。

Below is a working plunk to illustrate the idea (the data is different, but the concept is the same). 下面是一个可行的例子,以说明这个想法(数据不同,但概念相同)。

http://plnkr.co/edit/mN6BG6skUxX4yHHbBIn6?p=preview http://plnkr.co/edit/mN6BG6skUxX4yHHbBIn6?p=preview

I've been following your app due to the questions you made before, and I have something here for you: 由于您之前提出的问题,我一直在关注您的应用程序,在此为您提供一些帮助:

                  <p class="points-p"
                     ng-if="noDropdown"
                     ng-init="currentLine = getCurrentLine(slip)">
                      {{currentLine.pick}}
                  </p>

and then in currentLine you should put something like .pick or I guess .team , isn't it ? 然后在currentLine您应该放置.pick类的.pick或者我猜是.team ,不是吗?

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

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