简体   繁体   English

使用ngClipboard的Angular控制器-错误:未知的提供程序

[英]Angular Controller Using ngClipboard - Error: Unknown provider

I'm trying to set an ng-click event inside of a controller so that when the user clicks a div the function is ran and compiles a string and adds it to the clipboard. 我正在尝试在控制器内设置ng-click事件,以便当用户单击div时运行该函数并编译字符串并将其添加到剪贴板。

I'm getting the following error though: 我虽然遇到以下错误:

angular.js:13550Error: [$injector:unpr] Unknown provider: ngClipboardProvider <- ngClipboard <- eventCtrl

HTML HTML

<div class="row">
    <div><div ng-click="vm.copy()" class="button tiny">Copy</div></div>
</div>

Angular Controller (updated) 角度控制器 (已更新)

(function() {

    angular
        .module('monitorApp', ['ngClipboard'])
        .controller('eventCtrl', eventCtrl);

    eventCtrl.$inject = ['$scope', '$routeParams', 'monitorData', 'ngClipboard'];
    function eventCtrl($scope, $routeParams, monitorData, ngClipboard) {
        var vm = this;

        vm.copy = function() {
            vm.copyjunk = "COPIED crap here!!";
            vm.copyjunk = ngClipboard.toClipboard;
        };
    }

})();

I have some other code in the controller that deals with a service I created, but for simplicity sake, I excluded it and only have the bare essentials for the ngClipboard. 我在控制器中还有一些其他代码可以处理我创建的服务,但是为了简单起见,我将其排除在外,仅具有ngClipboard的基本知识。

I've also tried many different variations of injecting ngClipboard into the controller, but it doesn't seem to work. 我还尝试了将ngClipboard注入控制器的许多不同变体,但似乎没有用。 Thanks for any help! 谢谢你的帮助!

(note: I have included clipboard.min.js and ngclipboard.min.js in my html) (注意:我在我的html中包含了clipboard.min.js ngclipboard.min.jsngclipboard.min.js

EDIT (SOLVED) 编辑(已解决)

HTML HTML

<div ngclipboard data-clipboard-text="COPIED TEXT HERE!" class="button tiny">Copy Dial-in</div>

Controller 调节器

Left it alone, no "ngclipboard" stuff here

app.js (NEW! - primary angular file) app.js (新功能-主角度文件)

angular.module('monitorApp', ['ngRoute','ngclipboard']);

So putting the new addition of ['ngclipboard'] into the main file and not a controller allowed the app to work properly. 因此,将新添加的['ngclipboard']添加到主文件中而不是添加到控制器后,该应用才能正常运行。 Before, the app would load, no errors, but entire page would be blank. 之前,该应用程序将加载,没有错误,但整个页面将为空白。 Special thanks to Shannon for the help. 特别感谢Shannon的帮助。

Make sure you include the source correctly like so into the bottom of body 确保像这样正确地将源包括在主体底部

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://cdn.rawgit.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script>
<script src="dist/ngclipboard.min.js"></script>

Looks like you've simply missed the module dependency. 看来您只是错过了模块依赖性。

angular.module('monitorApp'), ['ngclipboard']);

Not only that, but you've tried to use a provider called ngClipboard which does not exist in the source. 不仅如此,您还尝试使用名为ngClipboard的提供程序,该提供程序在源代码中不存在。

This ngclipboard module uses attribute directives only. 此ngclipboard模块仅使用属性指令。 HTML : HTML

<!-- Target -->
<input id="foo" value="https://github.com/sachinchoolur/ngclipboard.git">

<!-- Trigger -->
<button class="btn" ngclipboard data-clipboard-target="#foo">
    <img src="assets/clippy.svg" alt="Copy to clipboard">
</button>

This will copy the content from the input when the button is pressed (targets the id). 当按下按钮时(目标ID),这将从输入中复制内容。

In your case, if your vm.copy() method just returns a string, just do something like this: 在您的情况下,如果您的vm.copy()方法仅返回一个字符串,则可以执行以下操作:

<button class="btn" ngclipboard data-clipboard-text="{{vm.copy()}}">
    {{vm.copy()}}
</button>

The ngClipboard module is a directive only. ngClipboard模块仅是指令。 Bringing it into my controller breaks my app as well. 将其带入我的控制器也会破坏我的应用程序。 You just need to add the markup directly into your view: 您只需要将标记直接添加到视图中:


<button class="btn" ngclipboard data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
    Copy to clipboard
</button>

<button class="btn" ngclipboard data-clipboard-text="{{vm.copyjunk}}">
    {{vm.copyjunk}}
</button>

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

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