Angular.js+require.js+jQuery插件自定义指令

Angular.js + require.js + jQuery Plugin custom directive

本文关键字:自定义 指令 插件 js+jQuery js+require Angular      更新时间:2023-09-26

我是Angular和Require的新手,我的问题是关于以下代码(我已经连接了Angular和Require,如果被要求,我可以包括文件);引导开关元件未初始化:

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

谢谢!!!

语法问题1:

更改:

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

收件人:

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

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

语法问题2:

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

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

不可能在return语句之后返回另一个东西。如果require语句试图加载一些依赖项,可以将其放入define-first参数中。