Correct way of wrapping javascript class in AngularJS module and inject angular services

后端 未结 2 1694
误落风尘
误落风尘 2021-01-03 06:48

Inside an AngularJS module I\'m developing, I have a Canvas class defined as:

angular.module(\"myModule\", [])
.factory(\"Canvas\", function() {         


        
2条回答
  •  醉话见心
    2021-01-03 07:10

    I create Canvas service like this and is work:

    var app = angular.module('myModule', []);
    
    app.factory("Canvas", ["$q", function($q) {
    
        var Canvas = function(element, options) {
            this.q = $q;
    
            this.init();
            console.log(this.q, element, options);
        }
        Canvas.prototype.init = function() {/*...*/};
        Canvas.prototype.otherMethod = function() {/*...*/};
    
        return Canvas;
    }]);
    
    app.controller('MainCtrl', ['$scope', 'Canvas', function($scope, Canvas) {
        console.log( new Canvas().q );  
    }]);
    

    Also you can see this on Pluncer here

提交回复
热议问题