我正在尝试在AngularJS / RequireJS应用程序中使用ZeroClipboard。
我已将ZeroClipboard.js放入/assets/js/vendors/ZeroClipboard.js
我已经设置了RequireJS应用程序的main.js
main.js
requirejs.config({
paths: {
jQuery: 'vendors/jquery.min',
'clipboard': 'vendors/ZeroClipboard',
underscore: 'vendors/underscore-min',
angular: 'vendors/angular.min',
'angular-route': 'vendors/angular-route.min',
'controllers': 'controllers',
'services': 'services',
'filters': 'filters',
'directives': 'directives',
'app': 'app'
},
shim: {
underscore: {
exports: '_'
},
'jQuery': {
'exports': 'jQuery'
},
'angular': {
exports: 'angular'
},
'states': {
deps: ['angular'],
exports: 'states'
},
'angular-route': {
deps: ['angular']
}
},
priority: [
'angular'
]
});
requirejs(['angular',
'app',
'underscore',
'routes',
'vendors/jquery.min',
'services/services',
'directives/directives',
'filters/filters',
'controllers/controllers'
], function (angular, app, _) {
angular.element(document).ready(function () {
angular.bootstrap(document, ['App']);
document.getElementsByTagName('html')[0].dataset.ngApp = 'App';
});
});
在控制器中我正在调用ZeroClipboard
controller.js
define(['clipboard'], function() {
var AppCtrl = function($scope, $modal, $timeout, $log, $http, $routeParams, $rootScope) {
var client = new ZeroClipboard( $("li#copy-buildr") );
};
return AppCtrl;
});
我似乎得到的只是ReferenceError: ZeroClipboard is not defined
答案 0 :(得分:0)
尝试将controller.js
更改为
define(['clipboard'], function(ZeroClipboard) {
注意函数参数中的ZeroClipboard。
答案 1 :(得分:0)
使用指令并将ZeroClipboard.js
直接加载到DOM中已经奏效。不确定如何通过AMD加载该文件,因为它总是返回错误。
.directive('clipCopy', ['$window', function ($window) {
return {
scope: {
clipCopy: '&',
clipClick: '&'
},
restrict: 'A',
link: function (scope, element, attrs) {
// Create the clip object
var clip = new ZeroClipboard( element, {
moviePath: '//cdnjs.cloudflare.com/ajax/libs/zeroclipboard/1.1.7/ZeroClipboard.swf',
trustedDomains: ['*'],
allowScriptAccess: "always"
});
clip.on( 'mousedown', function(client) {
client.setText(scope.$eval(scope.clipCopy));
if (angular.isDefined(attrs.clipClick)) {
scope.$apply(scope.clipClick);
console.log($scope.clipClick);
}
});
}
}
}]);
答案 2 :(得分:0)
这对我有用:
require(["zeroClipboard"], $.proxy(function (ZeroClipboard) {
var client = new ZeroClipboard( $('.link-label') );
client.on( 'ready', function(event) {
client.on( 'copy', function(event) {
event.clipboardData.setData('text/plain', $(event.target).prev().html());
} );
client.on( 'aftercopy', function(event) {
console.log('Copied text to clipboard: ' + event.data['text/plain']);
} );
} );
client.on( 'error', function(event) {
ZeroClipboard.destroy();
} );
}, this));