【问题标题】:how can I set default value in ui-select如何在 ui-select 中设置默认值
【发布时间】:2019-04-15 17:00:18
【问题描述】:

我试过set ng-init并给出ng-model的初始值,都失败了

$scope.newSubItem = function (scope) {
    var modal = $uibModal.open({
        backdrop: 'static',
        templateUrl: 'catalog/inputForm/partial/add_new_softwareOnTree.html',
        scope: newScope,
        controller: ['$scope', '$uibModalInstance','_', function ($scope, 
                    $uibModalInstance,_) {
        $scope.nodeRelationships4CatalogAddingSoftware = 
                codeTableService.nodeRelationships4CatalogAddingSoftware;
        $scope.targetNodes4ContainedInOrDependsOn = 
                  targetNodes4ContainedInOrDependsOn;
        $scope.nodes = nodes;
        $scope.newSoftware = {};
        var targetNodeId = nodeData && nodeData.id;
        $scope.newSoftware.target = 
              _.find(targetNodes4ContainedInOrDependsOn,function (o) {
                   return o.name ===  targetNodeId;
              });
        $scope.componentsQueryUrl = 
                  CONSTANTS.SERVICE_BLUEPRINT_COMPONENTS.PATH + "?query";
        $scope.submit = function () {           
         catalogCustomizedSoftwareComponentConfigService.onAddSoftwareSubmit(
              $scope,
              $scope.newSoftware,
              $scope.targetNodes4ContainedInOrDependsOn);

        $uibModalInstance.close($scope.targetNodes4ContainedInOrDependsOn);
           };
     }]
});
<div class="form-group row">
    <label class="col-lg-2 col-md-2 col-sm-2 control-label required">{{'Relationships' | translate}}</label>
    <div class="col-lg-9 col-md-9 col-sm-9">
        <div class="input-group">
        <ui-select ng-model="newSoftware.relationship"name="nodeRelationships" 
             required>
        <ui-select-match placeholder="{{'Please select a 
          relationship'|translate}}">{{$select.selected.name|translate}}
        </ui-select-match>
        <ui-select-choices repeat="item.id as item in 
          nodeRelationships4CatalogAddingSoftware | translateItemFilter: { 
         name:$select.search}">
        <div ng-bind-html="item.name | translate | highlight: $select.search"> 
        </div>
        </ui-select-choices>
        </ui-select>
     </div>
<span class="error" ng-show="addSoftWareForm.nodeRelationships.$touched && addSoftWareForm.nodeRelationships.$error.required">
{{'Please select a relationship'|translate}}
</span>
</div>

codeTableService.nodeRelationships4CatalogAddingSoftware = 
[
{id: "cloudify.relationships.contained_in", name: "contained_in"},
{id: "cloudify.relationships.depends_on", name: "depends_on"}
]

我想设置ui-select的初始值,而该值不显示。 我希望将 ui-select 的初始值设置为“contained_in”

$scope.newSoftware.relationship = codeTableService.nodeRelationships4CatalogAddingSoftware[0].name

并且下拉列表包含两个值:

["contained_in", "installed_to"]

【问题讨论】:

  • nodeRelationships4CatalogAddingSoftware 对象是什么样的?它有一个我想和名字。他们是一样的吗? Contined_in 是我的名字和名字?我只问是因为你说下拉菜单显示 contains_in 是名字。 id是什么?
  • nodeRelationships4CatalogAddingSoftware 类型:数组

标签: angularjs angularjs-ng-model ui-select


【解决方案1】:

也许您只需要为模型分配初始值。我认为它不会显示在前端。

【讨论】:

  • 我无法理解,当我尝试将初始值分配给模型时,例如javascript $scope.newSoftware.relationship = "contained_in"; ,页面加载时 ui-select 将不显示任何内容。
  • 您正在设置 $scope.newSoftware.relationship = "contained_in"; 但在您的重复中,您将所选项目的值设置为 item.id,这不是“contained_in”。尝试设置$scope.newSoftware.relationship = nodeRelationships4CatalogAddingSoftware[0],id;的初始值,如果想改用名字,改repeat="item.name as item in nodeRelationships4CatelogAddingSoftware"就可以用名字了。
  • @KerouacJack 这不是你想要的行为吗?我误会了吗?
  • @kendavidson 我试过 $scope.newSoftware.relationship = codeTableService.nodeRelationships4CatalogAddingSoftware[0].name;而浏览器得到 TypeError: Cannot set property 'relationship' of undefined
  • 这意味着新软件​​未定义。你可以处理那个,只需使用对象表示法来设置它。
猜你喜欢
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
相关资源
最近更新 更多