search

Home  >  Q&A  >  body text

angular.js - AngularJS provider inline injection method registration service error

app.provider("myService",['$http',function(){
            return {
                myUrl:null,
                setUrl:function(url) {
                    myUrl = url;
                },
                $get:function($http) {
                    return {
                        getPerson:function() {
                            return $http({
                                method:"GET",
                                url:myUrl
                            });
                        }
                    }
                 }
            }
        }]);

This code reported such an error:

ionic.bundle.min.js:40 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.3/$injector/modulerr?p0=myApp&p1=Error%3A%2…st%3A63342%2FIonicDemo%2Flib%2Fionic%2Fjs%2Fionic.bundle.min.js%3A73%3A340

What kind of error is this? What is wrong with the above code

PHP中文网PHP中文网2794 days ago810

reply all(2)I'll reply

  • 阿神

    阿神2017-05-15 17:06:46

    Will your code be compressed? If so, it is probably because there is no explicit dependency injection after $get

    app.provider("myService",['$http',function($http){
                return {
                    myUrl:null,
                    setUrl:function(url) {
                        myUrl = url;
                    },
                    $get:['$http',function($http) {
                        return {
                            getPerson:function() {
                                return $http({
                                    method:"GET",
                                    url:myUrl
                                });
                            }
                        }
                     }]
                }
            }]);

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:06:46

    app.provider("myService",['$http',function(){
    Change the above to function($http)
    You forgot to write the parameters, so $injector error will be reported

    reply
    0
  • Cancelreply