2013-07-09 126 views
0

我是新的Angularjs ...只是生气了,使这一工作: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; }, 
      })); 
     }, 
    }; 
}); 

这是我的角度指令,这使得<div select-company></div>成选择2控制。它工作在此刻,它也返回的细节,但我得到这个错误:

My error

任何想法?

回答

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

非常感谢!我想你只是将函数()添加到$超时,对吧?它工作完美。 –

+0

是的,情况就是如此。 – finishingmove

+0

顺便说一句我用内联1行功能,使代码更清洁(对我来说) –

相关问题