AngularJS在定制服务中使用角度模块

Jer*_*lus 0 javascript angularjs angular-services angular-module

我有一个用于生成令牌的自定义服务,在这个服务中我需要使用MD5功能,MD5功能在模块中 angular-md5

这是我的服务代码,角度模块generate_token使用的函数md5.createhash()但是我得到了错误:TypeError: Cannot read property 'createHash' of undefined

我想我做错了但我看不清楚

angular.module('app.services', ['angular-md5'])

.service('Auth', [function($scope, md5){
    var self = this;

    this.rand_alphanumeric = function() {
        var subsets = [];
        subsets[0] = [48, 57]; // ascii digits
        subsets[1] = [65, 90]; // ascii uppercase English letters
        subsets[2] = [97, 122]; // ascii lowercase English letters 

        // random choice between lowercase, uppercase, and digits
        s = Math.floor(Math.random() * 2) + 0
        ascii_code = Math.floor(Math.random() * (subsets[s][1] - subsets[s][0] + 1) + subsets[s][0]);
        return String.fromCharCode(ascii_code);
    }

    this.generateToken = function() {
        var str = "";
        for (var i = 0; i < 8; i++) 
            str += self.rand_alphanumeric();
        return md5.createHash(str);
    }
}]);
Run Code Online (Sandbox Code Playgroud)

Sac*_*tin 5

您没有正确使用依赖项数组注入

试试这个

angular.module('app.services', ['angular-md5'])

.service('Auth', ['$scope','md5',function($scope, md5){
var self = this;
Run Code Online (Sandbox Code Playgroud)