Angularjs指令TypeError:object不是函数

时间:2013-07-09 12:08:08

标签: angularjs angularjs-directive

我是Angularjs的新人......为了让这件事发挥作用,我生气了:

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

这是我的Angular指令,它将<div select-company></div>转换为select2控件。它目前有效,它也会返回详细信息,但我收到此错误:

My error

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通常,您希望在开发期间使用非缩小版本的脚本,因为它们提供了更具描述性的堆栈跟踪...

很难说这到底发生了什么,但请尝试:

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