【问题标题】:Angular JS 1.3.11: Argument 'MyController' is not a functionAngular JS 1.3.11:参数“MyController”不是函数
【发布时间】:2015-01-30 18:43:42
【问题描述】:

我是 Angular 新手,我有一个问题。

我使用的是 1.3.11 版本的 angular。

我写了一个简单的 html 代码,它使用简单的角度,我得到以下错误:

参数 'MyController' 不是函数,在 AngularJS 中未定义 [重复]

html代码是:

<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js">
 </script>
</head>
<body>

<div ng-controller = "MyController">
  <h1> {{author.name}}</h1>
  <p> {{author.title + ', ' + author.company}}</p>
</div>

<script>
  function MyController($scope)
  {
    $scope.author= {
      'name' : 'Naim',
      'title' : 'MR',
      'company' : 'Naimos'
    }
  }
</script>

</body>
</html>

提前谢谢你。

【问题讨论】:

  • 好吧,新的角度 - 试试这个docs.angularjs.org/tutorial。您的 sn-p 缺少角度基础知识...教程应该有所帮助
  • 我不知道问题是什么,但想说不要在 html 中的赋值之间放置空格,如下所示:ng-controller = "MyController" 应该是 ng-controller="MyController"
  • @RadimKöhler OP 在头部包含了角度...
  • docs.angularjs.org/guide/migration 从 1.2 迁移到 1.3 $controller 将不再在窗口上寻找控制器。使用 angular.module('myApp', []).controller('MyController', [function() { // ... }]);

标签: javascript html angularjs


【解决方案1】:

working plunker

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <meta charset="UTF-8" />
    <title>Angular Demo</title>
    <script data-require="angular.js@*" data-semver="1.3.11" 
     src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.js"
    ></script>
  </head>

  <body>
    <div ng-controller="MyController">
      <h1> {{author.name}}</h1>
      <p> {{author.title + ', ' + author.company}}</p>
    </div>
    <script>
  function MyController($scope)
  {
    $scope.author= {
      'name' : 'Naim',
      'title' : 'MR',
      'company' : 'Naimos'
    }
  }
  angular
    .module('app', [])
    .controller("MyController", MyController)
    </script>
  </body>

</html>

检查角度模块的定义:

  angular
    .module('app', [])
    .controller("MyController", MyController)

此外,模块“app”现在被注入 int html

<html ng-app="app">

在行动中检查它here

【讨论】:

  • 无需使用 module(),因为 OP 正在使用 ng-controller,并且 OP 的代码也应该可以正常工作..
  • @BhojendraNepal 与 angular 1.3 不相符 docs.angularjs.org/guide/migration
【解决方案2】:

在 SO 上工作?!

这是引用 1.2

按照迁移示例让 ti 使用 1.3

https://docs.angularjs.org/guide/migration

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js">
 </script>
</head>
<body>

<div ng-controller = "MyController">
  <h1> {{author.name}}</h1>
  <p> {{author.title + ', ' + author.company}}</p>
</div>

<script>
  function MyController($scope)
  {
    $scope.author= {
      'name' : 'Naim',
      'title' : 'MR',
      'company' : 'Naimos'
    }
  }
</script>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2016-10-28
    • 2015-09-02
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多