md-select can't set selected value

后端 未结 7 1594
有刺的猬
有刺的猬 2020-12-29 04:59

I have a md-select set up as follows:


    

        
7条回答
  •  -上瘾入骨i
    2020-12-29 05:06

    In my case adding ng-model-options="{trackBy: '$value.id'}" as described in the docs did not work, as no initial value was set.

    By explicitly setting the model to the wished default value in the controller properly set the value as desired. This approach might be easier if you do not know up front the index of the element you want to display as pre-selected (using ng-selected). Of course you can evaluate it in the controller, but to me it seems more immediate to set the target element directly to the model.

    View:

    {{item.value}}

    Controller:

    function initialize() {
            vm.globalFilterValues = globalFilterValues;
            vm.selectedGlobalFilter = TransferGlobalFilter.Worldwide;
        }
    

    Where globalFilterValues are in the form:

    [
      {key:"All", value:"All values" },
      {key:"Manager", value:"Manager"},
      {key:"HR", value:"Human resources"},
    ]
    

提交回复
热议问题