简体   繁体   English

从AngularJS指令中的文件运行JavaScript代码

[英]Running JavaScript code from a file in AngularJS directive

I've made a directive in AngularJS for my footer section in a website that I load on every page. 我已经在每个页面加载的网站的页脚部分在AngularJS中做了一个指令。 Inside, I've added some jQuery code that needs to get run once the page is loaded - that was the only solution I could think of for doing that. 在内部,我添加了一些jQuery代码,页面加载后需要立即运行-这是我想到的唯一解决方案。 Here's an example of how it looks: 这是一个外观的示例:

app.directive('footer', function () {
    return {
        restrict: 'A',
        replace: true,
        scope: {user: '='}, 
        templateUrl: "PATH_TO_HTML_FILE",
        controller: ['$scope', '$filter', function ($scope, $filter) {
            // Some random jQuery goes here, for example:
            $('.search-nav > a').on('click', function(){
                $('.search-drop-down').slideDown();
            });

            // Close Search form
            function closeSearchField() {
                $('.search-drop-down').slideUp();
            }
        }]
    };
});

I'm not sure if that's the best way to do it but what I want to do now is get this jQuery code in the controller out in a separate file. 我不确定这是否是最好的方法,但是我现在要做的是将控制器中的jQuery代码放在单独的文件中。 The reason: the front-enders that we work with have no idea what AngularJS is not even to mention directive, etc. so I just want to have a separate file with all the jQuery code that gets run on page load that they can edit if needed. 原因是:与我们合作的前端人员甚至不知道什么AngularJS甚至不提及指令,等等。因此,我只想拥有一个单独的文件,其中包含所有页面加载时运行的jQuery代码,如果可以,它们可以编辑需要。 Once the directive itself gets loaded, I would just like to get the contect of the file and run it in the body of the controller up above. 一旦指令本身被加载,我只想获取文件的内容并在上面的控制器主体中运行它。 Any ideas on how to do that? 关于如何做到这一点的任何想法? Or even a better solution to the whole problem? 甚至可以更好地解决整个问题?

You can define the controller in another file and just reference it in the directive by name: 您可以在另一个文件中定义控制器,并仅在指令中按名称引用它:

Controller 控制者

app.controller('footerCtrl', footerCtrl);

//You are not actually using it in the controller, so you can remove it if you don't need it
footerCtrl.$inject = ['$scope', '$filter'];

function footerCtrl($scope, $filter){
    // Some random jQuery goes here, for example:
            $('.search-nav > a').on('click', function(){
                $('.search-drop-down').slideDown();
            });

            // Close Search form
            function closeSearchField() {
                $('.search-drop-down').slideUp();
            }
}

Directive 指示

app.directive('footer', function () {
    return {
        restrict: 'A',
        replace: true,
        scope: {user: '='}, 
        templateUrl: "PATH_TO_HTML_FILE",
        controller: 'footerCtrl'
});

You can also get rid of this code entirely and just use a css class for the animation, using ng-class to add/remove the class when clicking the link 您也可以完全摆脱此代码,仅使用css类制作动画,单击链接时使用ng-class添加/删除类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM