angularjs - ui-select 如果为空则禁用

标签 angularjs ui-select

我是 AngularJS 的初学者。我有一个应用程序,它进行 API 调用以获取汽车制造商、型号和年份的嵌套列表。我创建了三个 ui-select 元素来显示调用的结果,从汽车品牌列表开始,然后过滤到该品牌中的模型,最后是该模型的年份。我希望在为空时禁用第二个和第三个选择,以便用户无法尝试开始输入汽车模型。

<form class="form-horizontal" ng-controller="instant_quote" ng-submit="loadQuote()" name="quoteform">
    <div class="form-group" id="Make">
        <label for="Makes" class="col-md-3 control-label">Make</label>
        <div class="col-md-9">
            <ui-select
                    id="Makes"
                    ng-model="request.make">
                <ui-select-match placeholder="Enter a Make…">{{$select.selected.name}}</ui-select-match>
                <ui-select-choices repeat="choice in makes | filter: $select.search">
                    <div ng-bind-html="choice.name | highlight: $select.search"></div>
                </ui-select-choices>
            </ui-select>
        </div>
    </div>

    <div class="form-group" id="Model">
        <label class="col-md-3 control-label">Model</label>
        <div class="col-md-9">
            <ui-select
                    id="Models"
                    ng-model="request.model"
                    ng-disabled="request.make == 'false'">
                <ui-select-match placeholder="Enter a Model…">{{$select.selected.name}}</ui-select-match>
                <ui-select-choices repeat="choice in request.make.models | filter: $select.search">
                    <div ng-bind-html="choice.name | highlight: $select.search"></div>
                </ui-select-choices>
            </ui-select>
        </div>
    </div>

这就是我试图根据 Angular ui-select placeholder not working when ng-model is initalized 确定每个选择是否为空的方式

我的所有输入都没有被禁用。

最佳答案

我通常在 myArray.length 上使用 ng-disable 来获取 0 或 !0 并将其用作 false 和 true。一直为我工作。

<select ng-model="model.a"
   ng-options="value for a in array"
   ng-disabled="!array.length">
</select>

在你的例子中呢:
<ui-select
    id="Models"
    ng-model="request.model"
    ng-disabled="!request.make.length">
<ui-select-match placeholder="Enter a Model…">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="choice in request.make.models | filter: $select.search">
    <div ng-bind-html="choice.name | highlight: $select.search"></div>
</ui-select-choices>

在您当前的代码中 request.make 将返回 [] 而不是 'false' ...如果我理解得很好。

关于angularjs - ui-select 如果为空则禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29534698/

相关文章:

css - 为什么背景图像的 ng-style 不起作用?

html - 为 ui-select 的每个选项添加不同的 CSS

javascript - 如何为angular ui-select设置默认值

javascript - 在ui-select中获取所选选项的索引 - Angularjs

javascript - 当值为 false 时在 ng-bind-html 上显示图标

AngularJS ui-select 组件,多选+远程服务器

javascript - AngularJS limitTo 与模型中的值 (JSON)

javascript - Angular-ui datepicker 在输入字段中输入时不允许使用 dd.MM.yyyy 格式

angularjs - 删除所有芯片后不显示自动完成建议(md-chips + md-autocomplete)

javascript - 如何在 AngularJS 中使用 CKEditor 上传图像?