2014-10-10 58 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