Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not watching model change in multiple #33

Open
piernik opened this issue Oct 6, 2015 · 0 comments
Open

Not watching model change in multiple #33

piernik opened this issue Oct 6, 2015 · 0 comments

Comments

@piernik
Copy link

piernik commented Oct 6, 2015

Now You have code for watching of external model change:

controller.$render = function () {
    getSelection(function (selection) {
        if (isMultiple) {
            element.select2("data", selection);
        } else {
            element.select2("val", selection.id);
        }
    });
};

Problem is that it works only on strings or numbers: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

If we have multiple option turned on and make external change of model nothing happens.
Here is my workaround:

if (isMultiple) {
    scope.$watch(function () {
        return controller.$modelValue;
    }, function (newVal, oldVal) {
        if (newVal !== oldVal) {
            getSelection(function (selection) {
                if (isMultiple) {
                    element.select2("data", selection);
                } else {
                    element.select2("val", selection.id);
                }
            });
        }
    }, true);
} else {
    controller.$render = function () {
        getSelection(function (selection) {
            if (isMultiple) {
                element.select2("data", selection);
            } else {
                element.select2("val", selection.id);
            }
        });
    };
}

If is multiple then use deep watcher - if not stay with $render

EDIT:
I see that initial value for multiple is not working also - controller.$render(); in $timeout Here You have to trigger:

getSelection(function (selection) {
                    if (isMultiple) {
                        element.select2("data", selection);
                    } else {
                        element.select2("val", selection.id);
                    }
                });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant