问题
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