Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:05:44+00:00 2026-06-14T10:05:44+00:00

My app.js contains var app = angular.module(‘myApp’, []). config([‘$routeProvider’, function ($routeProvider, $http) { …

  • 0

My app.js contains

var app = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider, $http) {
    ...
}]);

Service looks like

app.service('MyService', function () {
    addNums = function (text) {
        return text + "123";
    }
});

And in contoller I have

function adminCtrl ($scope, MyService) {
    $scope.txt = MyService.addNums("abc");
};

They are all in separate files. The problem is that I’m getting an error
Unknown provider: MyServiceProvider <- MyService

Looks like I’m doing something wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:05:45+00:00Added an answer on June 14, 2026 at 10:05 am

    The provider error can occur if you forgot to tell Angular to load your myApp module. E.g., do you have this in your index.html file?:

    <html ng-app="myApp">
    

    Your service is missing “this.”:

    this.addNums = function(text) {
    

    Fiddle.


    There seems to be a lot of confusion in the Angular community about when to use service() vs factory(), and how to properly code them. So, here’s my brief tutorial:

    The service() method expects a JavaScript constructor function. Many Angular code examples that use service() contain code that is not a constructor function. Often, they return an object, which kind of defeats the purpose of using service() — more about that below. If an object needs to be created and returned, then factory() can be used instead. Often, a constructor function is all that is needed, and service() can be used.

    The quotes below are from different AngularJS Google Group posts:

    The main difference between using factory() vs service() is that factory()
    must return an object, while service() doesn’t return anything but it
    must be an object constructor function.

    Use factory() if the function you are providing builds the object you
    want. I.e., Angular will essentially do
    obj = myFactory()

    to get the obj. Use service() if the function you are providing is
    a constructor for the object you want. I.e., Angular will essentially do
    obj = new myService()

    to get/instantiate the obj.

    So when people use service() and its code “return”s an object, it is kind of a waste because of the way JavaScript “new” works: “new” will first create a brand new JavaScript object (then do stuff with prototype, then call the function defined by myService(), etc. — details we don’t really care about here), but because the function defined by myService() returns its own object, “new” does something a bid odd: it throws away the object is just spent time creating and returns the object that the myService() function created, hence the “waste”.

    One of the reasons that service() was introduced was to make it easy
    to use “classical” OO techniques, such as defining your service as a
    coffeescript class.

    Also, the undocumented naming convention for services seems to be camelCase with first letter lowercased: e.g., myService.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've developed custom HTTP module that processes .aspx page (it just sets app.Response.Filter for
A small portion of my app contains a view were people can take brief
I have a simple metro app contains a button, a label and a drop
I'm writing a C++ application for Win32 in VS 2010 Pro. The app contains
This is a toy project I'm working on at the moment. My app contains
I have an app that contains several activities...so lets say user is navigating activity
I have an app which contains a scrollview with several tableviews. Each tableview is
I've created an app that contains 4 viewcontroller and its .h,.m files...In my firstviewcontroller
I've created an app which contains form and that have to filled up in
I am building an app which contains a list of addresses. This app is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.