AngularUI select2 onChange method

戏子无情 提交于 2019-12-06 23:40:54

Good thing I did just this in my project:

HTML

<select data-placeholder="Select an asset" class="input-xxlarge" ui-select2="sourceAssetId" ng-model="sourceAssetId" ng-options="asset.id as asset.name for asset in assets"></select>

Directive

module.directive("uiSelect2", function() {
    var linker = function(scope, element, attr) {
        element.select2();

        scope.$watch(attr.ngModel, function(newValue, oldValue) {
            console.log("uiSelect", attr.ngModel, newValue, oldValue);

            // Give the new options time to render
            setTimeout(function() {
                if(newValue) element.trigger("change");
            })
        });
    }

    return {
        link: linker
    }
});

Relevant Controller Code

$scope.$watch("sourceAssetId", function(newValue, oldValue) {
    if(newValue) $scope.fetchAsset();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!