javascript - Angular JS 1.3.11 : Argument 'MyController' is not a function

标签 javascript html angularjs

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

我使用的是 1.3.11 版本的 angular。

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

Argument 'MyController' is not a function, got undefined in AngularJS [duplicate]

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>

提前谢谢你。

最佳答案

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模块的定义:

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

而且,模块“app”现在被注入(inject)到 html 中

<html ng-app="app">

实际检查 here

关于javascript - Angular JS 1.3.11 : Argument 'MyController' is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242616/

相关文章:

javascript - AngularJS:使用 ng Repeat 和 ngonInit 从列表中删除项目

javascript - 不返回任何内容的JS测试函数

javascript - react 计算器

javascript - Redux-saga 不等待 API 的响应

html - 我可以用 CSS 实现吗?

html - 在各种分辨率下测试网络

javascript - 单击时如何更改按钮的功能?

javascript - JSON stringify 正在添加反斜杠

javascript - 两种方式绑定(bind)不适用于带有angularjs的模态(子 Controller )中的变量

javascript - 在 CodeIgniter 中寻找一些 Javascript 类的好例子