0

I'm new at Angularjs... Just got mad making this to work:

angular.module('app', ['ui.select2']).directive("selectCompany", function($timeout) {

    return {
        restrict: 'A',
        replace: true,
        template: '<input type="text" name="company_id" ng-model="companySelected" />',
        scope: {},

        link: function (scope, element, attrs, ctrl) {
            $timeout(element.select2({
                placeholder         : "Buscar empresa", minimumInputLength : 3, allowClear : true,
                ajax: {
                    url                 : 'http://' + window.location.host + '/ajax/module/company/load-companies',
                    dataType            : 'json',
                    type                : 'post',
                    quietMillis         : '250',
                    data                : function (term, page) { return { name: term }; },
                    results             : function (data, page) { return { results : data }; }
                },
                formatResult    : function(item) { return item.name; },
                formatSelection : function(item) { return item.name; },
                escapeMarkup    : function (m) { return m; },
            }));
        },
    };
});

This is my Angular directive which makes a <div select-company></div> into a select2 control. It works at the moment, it also return the details but I'm getting this error:

My error

Any idea?

1 Answer 1

1

Normally, you'd want to work with non-minified versions of scripts during development, because they give more descriptive stack traces...

Hard to say what exactly is going on here, but try:

$timeout(function () {
  element.select2({
    placeholder: "Buscar empresa",
    minimumInputLength: 3,
    allowClear: true,
    ajax: {
      url: 'http://' + window.location.host + '/ajax/module/company/load-companies',
      dataType: 'json',
      type: 'post',
      quietMillis: '250',
      data: function (term, page) {
        return {
          name: term
        };
      },
      results: function (data, page) {
        return {
          results: data
        };
      }
    },
    formatResult: function (item) {
      return item.name;
    },
    formatSelection: function (item) {
      return item.name;
    },
    escapeMarkup: function (m) {
      return m;
    },
  })
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot!! I think you just added the function() to the $timeout, right?. It worked perfectly.
Btw I'm used to inline 1 line functions to make the code cleaner (for me ;))
When I copy pasted it, it was messed up so I had to run it through a beautifier :)

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.