0

Can someone please tell me what's wrong with the below code factory part ? I get an error message for the third console log. Thanks in advance.

fsp.html:95 Uncaught SyntaxError: Unexpected token (
    angular.js:36Uncaught Error: [$injector:modulerr]       http://errors.angularjs.org/1.2.23/$injector/modulerr?p0=MyModule&p1=Error%…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.23%2Fangular.min.js%3A17%3A415)
      at angular.js:36
      at angular.js:3906
      at r (angular.js:325)
      at e (angular.js:3872)
      at gc (angular.js:3812)
      at c (angular.js:1444)
      at fc (angular.js:1459)
      at Xc (angular.js:1368)
      at angular.js:21949
      at HTMLDocument.a (angular.js:2573)
  <html>
    <head>
        <script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

</head>
<body ng-app="MyModule">
 <div ng-controller="MyController">

 </div>
 <script>
    var mod = angular.module('MyModule',[]);
    mod.provider("myProvider",function()
    {
        this.$get = function()
        {
            return "MyValue";
        };
    });
    mod.service("myService",function()
    {
        this.myFunc = function()
        {
            return "MyValueService";
        };
    });
    mod.factory("myFactory",function()
    {
        return
        {
            myFunc :function()
            {
                return "MyValueFactory";
            }
        }
    });
    mod.controller("MyController",function(myProvider,myService,myFactory)
    {
        console.log("MyController-myProvider"+myProvider);
        console.log("MyController-myService"+myService.myFunc());
        console.log("MyController-myFactory"+myFactory.myFunc());
    });
 </script>
</body>

0

3 Answers 3

3

You're falling foul of JavaScript's ASI. Change your factory's return statement to...

return { // note the brace is on the same line here
  // and the rest as normal

Working example ~ http://plnkr.co/edit/2CquJUcUU005F6CBpnky?p=preview

See also ~ https://stackoverflow.com/a/3218860/283366

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that worked. Does it matter only for the return ? I have the parentheses in the next line for service and provider and it works fine.
0

Do this..

 mod.factory('myFactory',function ()
    {
        return {
            myFunc:function () {
              return "MyValueFactory";
            }
        }
    });

Comments

0

indent your code likes as that

</head>
<body ng-app="MyModule">
 <div ng-controller="MyController">

 </div>
 <script>
    var mod = angular.module('MyModule',[]);
    mod.provider("myProvider",function()
    {
        this.$get = function() {
            return "MyValue";
        };
    });
    mod.service("myService",function()
    {
        this.myFunc = function() {
            return "MyValueService";
        };
    });
    mod.factory("myFactory",function()
    {
        return {
            myFunc :function() {
                return "MyValueFactory";
            }
        }
    });
    mod.controller("MyController",function(myProvider,myService,myFactory)
    {
        console.log("MyController-myProvider"+myProvider);
        console.log("MyController-myService"+myService.myFunc());
        console.log("MyController-myFactory"+myFactory.myFunc());
    });
 </script>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.