【问题标题】:Binding scope values to ui-select-match attributes in angular's ui-select将范围值绑定到 Angular 的 ui-select 中的 ui-select-match 属性
【发布时间】:2015-02-16 14:11:57
【问题描述】:

我正在使用 AngularUI 的 UI-Select 在我的应用程序中创建丰富的选择,我正在尝试将 allow-clear 属性绑定到范围内的函数或属性:

<ui-select ng-model="$parent.model" theme="bootstrap">
    <ui-select-match placeholder="..." allow-clear="$parent.allowClear">{{$select.selected.DisplayText}}</ui-select-match>
    <ui-select-choices repeat="opt.ID as opt in operators">
        {{opt.DisplayText}}
    </ui-select-choices>
</ui-select>

但无论我尝试什么,都无法正常工作。然后我在uiSelectMatch指令定义下的UI-Select源代码中找到了以下代码:

    $select.allowClear = (angular.isDefined(attrs.allowClear)) ? (attrs.allowClear === '') ? true : (attrs.allowClear.toLowerCase() === 'true') : false;

这可能意味着它正在对属性值进行字符串比较。

有什么方法可以解决这个问题并绑定属性(即使是在初始化期间的一次性绑定)?

【问题讨论】:

    标签: angularjs binding angular-ui angular-ui-select


    【解决方案1】:

    如果我说得对,您可以将您的值包装在 {{...}} 中以使其正常工作,如下所示:

    <ui-select-match placeholder="..." allow-clear="{{!!$parent.allowClear}}">{{$select.selected.DisplayText}}</ui-select-match>
    

    见小Demo

    【讨论】:

    • 谢谢,这行得通。对于未设置范围变量的情况({{!!allowClear}}),我需要添加双非运算符。这是因为 UI-Select 会将空属性的存在视为 true。
    • 好点!我已经更新了答案和演示,以防万一。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多