【问题标题】:how to show default dropdown value show top angularjs如何显示默认下拉值显示顶部 angularjs
【发布时间】:2016-12-28 12:17:54
【问题描述】:

我在卡片信息下方有一个下拉显示,我想在下面显示is_default= true 卡片显示顶部的其他卡片..请帮助我

<select  ng-model="approvedinvoicesCtrl.currentcard.card_id">
    <option ng-value="item.card_id" ng-repeat="item in approvedinvoicesCtrl.current_job.cards ">{{item.brand +'      '+ item.last_digits}}</option>
</select>


{brand:"Visa",
card_id:"card_19VHhAGdj8iWoXDURuRqNH28",
last_digits : 123123,
is_default:true},
{
brand:"Master",
card_id:"card_19VHhAGdj8iWoXDURuRqNH28",
last_digits : 123123,
is_default:false}

【问题讨论】:

标签: angularjs angularjs-scope angularjs-ng-repeat


【解决方案1】:

建议你这样做

 <select>
    <option ng-value="item.card_id" ng-repeat="item in dropdownval "
     ng-selected=item._default >
      {{item.brand +' '+ item.last_digits}}</option>
  </select>

LIVE DEMO

【讨论】:

  • 能否请您查看DEMO
  • @AnirudhMangalvedhekar 而不是评论,放弃你的答案。查看演示并投反对票。
  • 哦,没关系,它有 ng-selected..i upvoted..sorry for misunderstanding
  • 如果我把它改成默认值就不行了plnkr.co/edit/1pyYMCboKMJNDvkaXnRm?p=preview
  • @AddiKhan 你什么时候得到card_id??
【解决方案2】:

我建议在选项元素上使用ngOptions 而不是ng-repeat。

注意:ngOptions 为“option”元素提供了一种便利,当您希望将选择模型绑定到non-string 值时,应该使用它来代替ngRepeat。这是因为选项元素目前只能绑定到字符串值。有关更多信息,您可以查看此link。

使用 ngOptions 进行演示 :

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.current_job = {
      "cards": [
        {
          brand:"Visa",
          card_id:"card_19A",
          last_digits : 123123,
          is_default:true
        }, {
          brand:"Master",
          card_id:"card_19B",
          last_digits : 123123,
          is_default:false
        }        
      ]
    };
    
   for (var i in $scope.current_job.cards) {
     if ($scope.current_job.cards[i].is_default == true) {
       $scope.topCard = $scope.current_job.cards[i].card_id;
     }
   } 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <select ng-options="item.card_id as item.brand+' '+item.last_digits for item in current_job.cards"
   ng-model="topCard"> 
   </select>
</div>

使用 ngRepeat 进行工作演示 :

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.current_job = {
      "cards": [
        {
          brand:"Visa",
          card_id:"card_19A",
          last_digits : 123123,
          is_default:true
        }, {
          brand:"Master",
          card_id:"card_19B",
          last_digits : 123123,
          is_default:false
        }        
      ]
    }; 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <select ng-model="currentcard.card_id">
    <option ng-value="item.card_id" ng-repeat="item in current_job.cards" ng-selected="item.is_default">{{item.brand +' '+  item.last_digits}}</option>
  </select>
</div>

【讨论】:

  • card_id: ""charge_description: "cc"charge_type: "信用卡支付"proto: 对象
  • 你在哪里使用ng-click ?。不要试图更改原始帖子。我按照原帖回答了。
【解决方案3】:

您应该使用ng-init 和ng-options 作为默认值,例如:

<select ng-init="somethingHere = options[1]" 
        ng-model="somethingHere" 
        ng-options="option for option in options">
</select>

$scope.options = [
    'Brand1',
    'Brand2',
    'Brand3'        
];

【讨论】:

    【解决方案4】:

    你可以使用orderby

    <select ng-model="approvedinvoicesCtrl.currentcard.card_id><option ng-value="item.card_id" ng-repeat="item in approvedinvoicesCtrl.current_job.cards  | orderBy:'is_default':false">{{item.brand +''+ item.last_digits}}</option></select> 
    

    此处项目的“is_default”属性以相反的顺序排序。第二个参数可以是任意排序函数,所以可以按照任意规则排序。

    @link http://docs.angularjs.org/api/ng.filter:orderBy

    【讨论】:

    • 虽然这可能会解决问题,但由于使用过滤器而导致性能开销。过滤器在每个摘要周期执行一次。
    • 如果它导致摘要循环,为什么 Angular 应该将有序过滤器引入下拉列表。
    【解决方案5】:

    您可能更喜欢预先选择卡片(已选择选项),而不是使用顶部的默认卡片进行排序。

    ng-selected如果没有在ng-model中选择值,你可以使用默认值,否则,它会选择ng-model中的值。

    <select  ng-model="currentCard.selected" >
      <option 
          ng-value="item.card_id" ng-repeat="item in cards" 
          ng-selected="(currentCard.selected==item.card_id) || (!currentCard.selected && item.is_default)">
          {{item.brand +'      '+ item.last_digits}}
      </option>
    </select>
    

    或者您可以使用ng-options 方法。在最后一种情况下,您可以仅使用 ng-model 值预先选择一个选项。

    <select 
       ng-model="currentCard.selected" 
       ng-options="item.card_id as item.brand+' '+item.last_digits for item in cards">
    </select>
    

    看这两个例子的小提琴:

    https://jsfiddle.net/hdca55cg/

    如果您之前在模型中选择过,第二个显示默认卡。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多