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

时间:2022-05-01 19:36:55

I have a custom service for generating token, in this service I need to use an MD5 function, the MD5 function is in the module angular-md5

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

here is my service code, the generate_token function use md5.createhash() from the angular module but I'm getting ths error : TypeError: Cannot read property 'createHash' of undefined

这是我的服务代码,generate_token函数使用角度模块中的md5.createhash()但是我得到了错误:TypeError:无法读取未定义的属性'createHash'

I think I have made something wrong but I can't see what

我想我做错了但我看不出来

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);
    }
}]);

3 个解决方案

#1


5  

You are not using the dependency array injection properly

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

Try this

尝试这个

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

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

#2


0  

Just skip the brackets before function($scope, md5) and you should be fine. In angular you have different possibilites for the injection syntax. see: https://docs.angularjs.org/guide/di

只需跳过函数前的括号($ scope,md5),你应该没问题。在角度中,您有不同的注入语法可能性。请参阅:https://docs.angularjs.org/guide/di

#3


0  

You may have to define $scope and md5 in the array before function.

您可能必须在函数之前在数组中定义$ scope和md5。

e.g.

例如

    .controller('MainCtrl', ['$scope', 'md5', function($scope, md5) {
      $scope.$watch('email' ,function() {
        $scope.message = 'Your email Hash is: ' + md5.createHash($scope.email || '');
      });
    }]);

#1


5  

You are not using the dependency array injection properly

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

Try this

尝试这个

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

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

#2


0  

Just skip the brackets before function($scope, md5) and you should be fine. In angular you have different possibilites for the injection syntax. see: https://docs.angularjs.org/guide/di

只需跳过函数前的括号($ scope,md5),你应该没问题。在角度中,您有不同的注入语法可能性。请参阅:https://docs.angularjs.org/guide/di

#3


0  

You may have to define $scope and md5 in the array before function.

您可能必须在函数之前在数组中定义$ scope和md5。

e.g.

例如

    .controller('MainCtrl', ['$scope', 'md5', function($scope, md5) {
      $scope.$watch('email' ,function() {
        $scope.message = 'Your email Hash is: ' + md5.createHash($scope.email || '');
      });
    }]);