2014-10-10 57 views
2

我是新來的Angular和Require,我的問題是關於下面的一段代碼(我已經掛鉤角度和要求,如果問我可以包括文件);自舉開關元件未被初始化:Angular.js + require.js + jQuery插件自定義指令

define(['myapp.ui/module', function (module) { 
module.directive('bootstrapSwitch', ['$parse', '$path', function ($parse, $path) { 
    return { 
     restrict: 'A', 
     replace: true, 
     link: function (scope, element) { 
      //load requirements for this directive. 
      console.log(element.bootstrapSwitch); 
      element.bootstrapSwitch(); 
     } 
    }; 
    return require([ 
     'myapp.ui/switch/bootstrap-switch.min', 
     'css!myapp.ui/switch/bootstrap-switch.min'], 
     function() { 
      return { 
       restrict: 'A', 
       replace: false, 
       link: function (scope, element) { 
        //load requirements for this directive. 
        element.bootstrapSwitch(); 
       } 
      }; 
     }); 
}]); 

}]);

我能看到正在加載的指令文件(上面的那個),但該元素沒有初始化,我看不到bootstrap-switch.min.css和bootstrap-switch.min.js(我知道路徑的工作,我敢肯定,我有一個問題sintax的地方。如果沒有,請詳細說明。任何幫助,將不勝感激!!!!

謝謝!

回答

2

語法問題1:

變化:

define(['myapp.ui/module', function (module) { 

要:

define(['myapp.ui/module'], function (module) { 

而且,考慮使用比 '模塊',因爲它可以與AMD模塊混淆不同的參數名稱。 (所謂的「模塊」的模塊是指加載模塊本身就是一個特殊的模塊。)

語法問題2:

第二return聲明不會做任何事情。也許你希望包括在外部define所有依賴關係:

define([ 
    'myapp.ui/module', 
    'myapp.ui/switch/bootstrap-switch.min', 
    'css!myapp.ui/switch/bootstrap-switch.min' 
], function (ui) { 
+0

完美的解釋和發現我的錯誤!謝謝 :) – Jose 2014-10-10 22:58:10

0

無法返回後,另一件事return語句如果require語句嘗試加載某些依賴項,則可以將其放在define第一個參數中

+0

如:定義([ 'myapp.ui /模塊', 'myapp.ui /開關/自舉-switch.min',..])等等? – Jose 2014-10-10 22:50:39