【问题标题】:How to set a default value in ui-select in angular?如何在角度的ui-select中设置默认值?
【发布时间】:2016-12-01 18:18:11
【问题描述】:

我使用 ui-select 元素在 Angular 中编写了一个小程序。这是html代码:

  <ui-select ng-model="test.selectedContract">
    <ui-select-match >
      {{$select.selected.name}} - {{$select.selected.value}} ---- {{$select.selected.id.id}} *** {{$select.selected.policy.info.name }}    
    </ui-select-match>

    <ui-select-choices group-by="groupByLetter" 
    repeat="contract in (contracts |
    orSearchFilter: {id.id: $select.search, policy.info.name : $select.search} |
    orderBy: 'name') track by contract.name">
          {{contract.name}} - {{contract.value}} ---- {{contract.id.id}} *** {{contract.policy.info.name }}
    </ui-select-choices>
  </ui-select>

这是我的合同对象的结构:

    $scope.contracts = [{
      name: "contract1.00",
      value: 10,
      id: {
        id: 8000,
        code: 2
      },
      policy: {
        info: {
          name: "test1",
          country: "test"
        }
      }
    }, //other contract objects

最后是我的 plunker 代码:http://plnkr.co/edit/PigjGCj5ukxscJC7Wl97?p=preview

问题是我想将 "All" 显示为默认值(如果没有选择),而我的 plunker 中的默认值是:“ - ---- *** "

你能帮我设置这个默认值吗? 谢谢!

【问题讨论】:

    标签: angularjs ui-select


    【解决方案1】:

    您可以在控制器中定义一种 toString 函数并将其结果放在 ui-select 标记中,而不是硬编码破折号和星号。如果 value 为 null,则应返回“All”

    【讨论】:

      【解决方案2】:

      只需设置

         $scope.test.selectedContract = {
            name: "all",
            value: 10,
            id: {
              id: 8000,
              code: 2
            },
            policy: {
              info: {
                name: "test1",
                country: "test"
              }
            }
          } 
      

      DEMO

      【讨论】:

        猜你喜欢
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 2020-07-22
        相关资源
        最近更新 更多