2

I want to implement SelectBoxIt in my angularJS directive. Here is my directive's template html snippet(lookup.template.html):

<select class="selectBoxit" ng-model="ngModel">
    <option value="" disabled> {{placeholder}}</option>
    <option value='{{a.LookupCode}}' ng-repeat='a in lookups'>{{a.Name}}</option>
</select>

Isolated scope directive:

.directive('lookup', function(){
        return {
            restrict: 'E',
            scope: {
                lookups: "=lookups",
                ngModel: "=ngModel"
            },
            templateUrl: 'templates/lookup.template.html',
            link: function (scope, element, attrs) {
                scope.placeholder = attrs['placeholder'];
                $(".selectBoxit").selectBoxIt().on('open', function()
                {
//ScrollBar
$(this).data('selectBoxSelectBoxIt').list.perfectScrollbar();
                });
            }
        }
    });

Lookup Controller:

 .controller('LookupController', function($scope){
        $scope.schoolTypeCode = 'GNR';
        $scope.schoolTypes = [{  Name: "Kinder Garten",  LookupCode: "KG" },
            { Name: "Elemetary",  LookupCode: "ELM" },
            { Name: "High School",   LookupCode: "HSC" },
            { Name: "Preparatory",   LookupCode: "PRP" },
            { Name: "General",   LookupCode: "GNR" },
            {  Name: "Distance",  LookupCode: "DST"  }];
    });

and finally view using directive:

<lookup  id="cboSchoolType" lookups="schoolTypes"
                                 ng-model="SchoolTypeCode" placeholder="Select School Type"></lookup>

SelectBoxIt initialization worked fine but I noticed two problems. First, default value(ngModel) is not assigned and second, initialization is happenening before angular repeat finishes populating the options.This will result an empty select list and the following error message when it is clicked:

Uncaught TypeError: Cannot read property 'list' of undefined

So, are there any other ways to set the default value and trigger SelectBoxIt initialization after options are populated by angular repeat?

Thanks.

1
  • 1
    i think you need to use setTimeout inside the link function Commented May 19, 2015 at 10:16

1 Answer 1

1

try the "dropdown" instead of "list"

so instead of

$(this).data('selectBoxSelectBoxIt').list.perfectScrollbar();

try this line in your event function

$(this).data('selectBoxSelectBoxIt').dropdown.perfectScrollbar();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.