Angularjs directive TypeError: object is not a function

时光毁灭记忆、已成空白 提交于 2019-12-11 22:05:02

问题


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:

Any idea?


回答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;
    },
  })
});


来源:https://stackoverflow.com/questions/17547827/angularjs-directive-typeerror-object-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!