【问题标题】:Angular.js + require.js + jQuery Plugin custom directiveAngular.js + require.js + jQuery Plugin 自定义指令
【发布时间】:2014-10-10 22:32:50
【问题描述】:

我是 Angular 和 Require 的新手,我的问题是关于以下代码(我已经连接了 Angular 和 require,如果被问到我可以包含文件); bootstrap-switch 元素没有被初始化:

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 问题。如果没有,请详细说明。任何帮助将不胜感激!!!!

谢谢!!!

【问题讨论】:

    标签: javascript jquery angularjs twitter-bootstrap


    【解决方案1】:

    语法问题1:

    变化:

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

    收件人:

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

    此外,请考虑使用与“模块”不同的参数名称,因为它可能与 AMD 模块混淆。 (名为“module”的模块是一个特殊的模块,指的是加载的模块本身。)

    语法问题2:

    第二个return 语句不会做任何事情。也许您想在外部 define 中包含所有依赖项:

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

    【讨论】:

      【解决方案2】:

      在 return 语句之后不可能返回另一个东西。如果 require 语句试图加载一些依赖,你可以把它放在第一个参数中。

      【讨论】:

      • 如:define(['myapp.ui/module', 'myapp.ui/switch/bootstrap-switch.min', ..]) 等?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多