工厂角

时间:2017-01-23 03:53:59

标签: angularjs angular-factory

有人可以告诉我下面的代码工厂部分有什么问题吗?我收到第三个控制台日志的错误消息。提前谢谢。

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>

3 个答案:

答案 0 :(得分:3)

你的JavaScript ASI犯了错误。将工厂的退货单更改为......

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

工作示例〜http://plnkr.co/edit/2CquJUcUU005F6CBpnky?p=preview

另见〜https://stackoverflow.com/a/3218860/283366

答案 1 :(得分:0)

这样做..

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

答案 2 :(得分:0)

缩进你的代码就像那样

</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>