【问题标题】:Adding Angular plugin to controller将 Angular 插件添加到控制器
【发布时间】:2017-06-02 17:35:46
【问题描述】:

我有一个带有角度的 .net 项目。我正在尝试将 angular-hotkeys (https://github.com/chieffancypants/angular-hotkeys/) 添加到项目中并使用它。

我使用 nuget 将其添加到项目中,并将其添加到我的包中。

然后我将热键添加到需要依赖项的控制器并尝试添加功能。

angular.module('mainApp')
    .controller('ndcController', ["$scope", "$location", "$timeout", "fieldService", "entityService", "$filter", "authorizedUserService", "$q", "gerimedService", function ($scope, $location, $timeout, fieldService, entityService, $filter, authorizedUserService, $q, gerimedService,hotkeys) {
        hotkeys.add({
            combo: 'ctrl+up',
            description: 'This one goes to 11',
            callback: function () {
                var test = "";
            }
        });
    }]);

页面只是挂起加载,Ajax 永远不会完成。如果我删除函数 (hotkeys.add()),即使添加了热键依赖项,页面也会加载。

我还在 BundleConfig.cs 中添加了依赖项:

 public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-1.12.2.js",
                        "~/Scripts/modernizr-2.6.2.js",
                        "~/Scripts/respond.js",
                        "~/Scripts/angular.js",
                        "~/Scripts/angular-route.js",
                        "~/Scripts/angular-animate.js",
                        "~/Scripts/knockout-3.1.js",
                        "~/Scripts/knockout.mapping-latest.js",
                        "~/JavaScript/validateNumberInput.js",
                        "~/JavaScript/Accounting.js",
                        "~/Scripts/jquery-ui-1.8.24.js",
                        //"~/Scripts/jquery.validate.js",
                        "~/Scripts/moment.js",
                        "~/Scripts/bootstrap.js",
                        "~/Scripts/bootstrap-datepicker.js",
                        "~/Scripts/hotkeys.js",
                        "~/Scripts/mask.js"//below is my code
                                           //"~/JavaScript/Module/Module.js",
                                           //"~/JavaScript/Filters/Filters.js",
                                           //"~/JavaScript/Services/Services.js",
                                           //"~/JavaScript/Directives/Directives.js",
                                           //"~/JavaScript/Controllers/HomeControllers.js"
                        ));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            //bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
            //            "~/Scripts/modernizr-*"));
            //,"~/Content/site.css"
            bundles.Add(new StyleBundle("~/Content/css").Include(
                    "~/Content/bootstrap.css", "~/Content/site.css", "~/Content/hotkeys.css"
                ));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));

            //bundles.Add(new ScriptBundle("~/js/app").Include("~/JavaScript/ApprovalsDisplay.js"));
            BundleTable.EnableOptimizations = false;
        }
    }

我做错了什么,我该如何解决?

编辑:控制台错误:

"Error: [$injector:unpr] Unknown provider: hotkeysProvider <- hotkeys
http://errors.angularjs.org/1.2.28/$injector/unpr?p0=cfp.hotkeysProvider%20%3C-%20cfp.hotkeys
minErr/<@http://localhost:51059/Scripts/angular.js:78:12.....

【问题讨论】:

    标签: javascript .net angularjs


    【解决方案1】:

    我认为您的问题出在您的控制器上。缺少热键依赖项

    angular.module('mainApp', ['ui.mask', "ngRoute", "cfp.hotkeys"])
        .controller('ndcController', ["$scope", "$location", "$timeout", "fieldService", "entityService", "$filter", "authorizedUserService", "$q", "gerimedService","hotkeys", function ($scope, $location, $timeout, fieldService, entityService, $filter, authorizedUserService, $q, gerimedService,hotkeys) {
            hotkeys.add({
                combo: 'ctrl+up',
                description: 'This one goes to 11',
                callback: function () {
                    var test = "";
                }
            });
        }]);
    

    【讨论】:

    • 嗨,我试过了,没有 hotkeys.add() 函数,它仍然挂起。
    • 是的,让我把它添加到问题中。
    • 问题是您需要将此依赖项添加到控制器“cfp.hotkeys”而不是“热键”我已经更新了我的答案
    • 你说的很有道理。我查看了该文件,并且 cfp.hotkeys 就是它的名称。但是,我仍然遇到同样的问题:Error: [$injector:unpr] Unknown provider: cfp.hotkeysProvider &lt;- cfp.hotkeys
    • 你有 index.html 文件吗?如果你有它,你可以包含一个脚本标签来加载这个 angular-hotkeys js 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多