2013-07-09 431 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行功能,使代碼更清潔(對我來說) –

相關問題