Error: Argument is not a function, got undefined

前端 未结 17 1715
不思量自难忘°
不思量自难忘° 2020-12-07 19:35

Using AngularJS with Scala Play, I\'m getting this error.

Error: Argument \'MainCtrl\' is not a function, got undefined

I\'m try

相关标签:
17条回答
  • 2020-12-07 20:21

    Could it be as simple as enclosing your asset in " " and whatever needs quotes on the inside with ' '?

    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
    

    becomes

    <link rel="stylesheet" media="screen" href="@routes.Assets.at('stylesheets/main.css')">
    

    That could be causing some problems with parsing

    0 讨论(0)
  • 2020-12-07 20:21

    There appear to be many working solutions suggesting the error has many actual causes.

    In my case I hadn't declared the controller in app/index.html:

    <scipt src="src/controllers/controller-name.controller.js"></script>
    

    Error gone.

    0 讨论(0)
  • 2020-12-07 20:22

    Because this pops-up in Google when trying to find an answer to: "Error: Argument '' is not a function, got undefined".

    It's possible that you are trying to create the same module twice.

    The angular.module is a global place for creating, registering and retrieving AngularJS modules.

    Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module

    Source: https://docs.angularjs.org/api/ng/function/angular.module#overview

    Example:

    angular.module('myApp', []) Is used to create a module without injecting any dependencies.

    angular.module('myApp') (Without argument) is used to get an existing module.

    0 讨论(0)
  • 2020-12-07 20:24

    Уmed's second point was my pitfall but just for the record, maybe it's helping someone somewhere:

    I had the same problem and just before I went nuts I discovered that I had forgotten to include my controller script.

    As my app is based on ASP.Net MVC I decided to keep myself sane by inserting the following snippet in my App_Start/BundleConfig.cs

    bundles.Add(new ScriptBundle("~/app").Include(
                    "~/app/app.js",
                    "~/app/controllers/*.js",
                    "~/app/services/*.js" ));
    

    and in Layout.cshtml

    <head>
    ...
       @Scripts.Render("~/app")
    ...
    </head>
    

    Now I won't have to think about including the files manually ever again. In hindsight I really should have done this when setting up the project...

    0 讨论(0)
  • 2020-12-07 20:26

    Remove the [] from the name ([myApp]) of module

    angular.module('myApp', [])
    

    And add ng-app="myApp" to the html and it should work.

    0 讨论(0)
提交回复
热议问题