3

I'm using AngularUI's UI-Select to create rich selects in my application, and I'm trying to bind the allow-clear attribute to a function or property on the scope:

<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>

But no matter what I try, couldn't get it to work. Then I found the following code on UI-Select source code, under uiSelectMatch directive definition:

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

that could mean it's doing a string comparison for the attribute value.

Is there any way that I can work around this and bind the attribute (even a one-time binding during the initialization)?

1 Answer 1

4

If I got you right, you could just wrap your value in {{...}} to make it work, like this:

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

See little Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works. I needed to add double-not operator thou, for the cases that the scope variable is not set ({{!!allowClear}}). This is because UI-Select will treat existence of empty attribute as a true.
Good point! I've updated the answer and demo, just in case. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.