【问题标题】:Angularjs controller initializationAngularjs 控制器初始化
【发布时间】:2015-01-23 06:13:56
【问题描述】:

我正在学习 AngularJs。我正在 Plunker 上创建我的第一个应用程序,但它无法初始化控制器。我到底错过了什么?

Here is the link of my plunker project

    <html ng-app>

<head>
  <script data-require="angular.js@*" data-semver="1.4.0-beta.1" src="https://code.angularjs.org/1.4.0-beta.1/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="testcontroller">
  <h1>Hello {{message}}</h1>
</body>

</html>

var testcontroller = function($scope) {
  $scope.message = "world";
};

【问题讨论】:

  • ..这里你需要给出应用名称..
  • 只要我不使用模块就没有必要
  • 你需要它的人.. 去 AngulaJS 的文档
  • 我明白了。使用不带模块名称的 ng-App 与以前的版本(如 1.3.0-beta.5)完美配合,但 Angular 不再支持此功能

标签: javascript angularjs declaration angularjs-controller angularjs-module


【解决方案1】:

这里是Plunkr

声明ng-app="myApp"

你以错误的方式定义控制器

angular.module('myApp',[])
.controller('testcontroller', function($scope){
  $scope.message="world";
});

【讨论】:

  • 是否必须为应用程序分配模块?因为我见过很多只使用 ng-app 的例子
  • 模块化方法更好地分离代码docs.angularjs.org/guide/module
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
相关资源
最近更新 更多