1

I am trying to create an angular directive that will initialise the select2, this directive will be used in select tag as below

<select ng-model="add.doctype" class="form-control select2" select2 append-to-body ng-options="x.tipe for x in ::Types">
    <option value=""></option>
</select>

And the directive is

(function() {
    'use strict';

    angular.module('myapp.theme')
        .directive('select2', select2);

    function select2() {
        return {
            $(".select2").select2({ theme: "bootstrap" });
        };
    }

})();

it gave an error in console Unexpected string pointed to $(".select2").select2({ theme: "bootstrap" });, did i miss something there?

3
  • Can you check by removing 'use strict';? Commented Jun 15, 2016 at 6:41
  • return { ... } indicates you are returning an object but it has no keys. Try `return { link: function(scope, element) { jQuery(element[0]).select2({theme: 'bootstrap'}) } } Commented Jun 15, 2016 at 6:43
  • removing use strict do not solve the problem. Commented Jun 15, 2016 at 6:48

2 Answers 2

2

As pointed out eschie, you should use the link function to have your code working.

However as eschie did there is no point using a directive/ Because this JQuery code will select all element with the class 'select2' for each time angular will find it since the directive wil get executed each time angular will find the select2 class.

You should do it like this :

angular.module('myapp.theme').directive('select2', select2)
function select2() {
    return{
        restrict: 'A',
        link: function(scope, element, attr){
            $(element).select2({ theme: "bootstrap" });
        }
    }
}

Note that i restrict on attribute. I noticed you have select2 in your select tag as a class and as an attribute. In angular way it is better to use an attribute as a directive. If you want to keep the class one use restrict:'C' in the directive.

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

4 Comments

it give an error $(...).select2 is not a function
@JosephGoh Did you include the select2 library in your index.html file ?
i am using bower and it supposes to add the library automatically right. but thank for the time and effort guys, really appreciated. I have found angular-ui-select which does the job. again thank you all.
@JosephGoh you should say this as an answer or at least an edit so people will know that for you the problem is gone.
1

1) Check to make sure jQuery is included in our project.

2) Initialize the directive to be used as class selector. From Angular's directive documentation:

Note: When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option.

3) Put your logic in the directive's link function:

angular.module('myapp.theme')
.directive('select2', select2)

function select2() {
    return{
        restrict: 'C',
        link: function(){
            $(".select2").select2({ theme: "bootstrap" });
        }
    }
}

4 Comments

OP appears to be using the attribute select2 as well as the class
Additionally, you should probably pass in the element to the link function if you have more than 1 ".select2" element
modified the directive, and change the directive name to select21, it is not give any error nor it initialize the select tag.
change restrict to 'CA' to initialize directive on both class and attribute. also, make sure the name of the directive, class, and/or attribute tag all match up.

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.