oop classes in an Angular service

2019-08-05 13:28发布

Anyway I think these are oop classes, I'm new to this. I'm trying to move some complex stuff written by someone else into an Angular service and I can't seem to get the syntax right. I have something like this:

angular.module('myApp')
    .service('myService', function () {

    var service = this;

    this.Util = {

     extend: function (blah) {
       //blah blahh
       return blah

    },

     create: Object.create || (function (blah) {

       //blah blah
        return blah

    })(),

     //more blah blah

   };


    var something = this.Util.create(blah);

});

I hope that's enough detail to tell what I'm doing wrong. It's telling me it 'Cannot read property 'create' of undefined'.

1条回答
疯言疯语
2楼-- · 2019-08-05 14:04

i will try to give a you a way to do it :

service

angular.module('myApp').service('Util', [function () {

     this.extend = function (blah) {
       //blah blahh
       return blah

    };

     this.create: function (blah) {

       //blah blah
        return blah

     },
     // should write new methods like this.myMethod
}]);

controller

angular.module('myApp').controller('myCtrl',[
    'Util',
    function(UtilSvc) {
        var something = UtilSvc.create(); // call your service method
    }
]);
查看更多
登录 后发表回答