AngularJS简单示例在Plunker中不起作用

时间:2015-05-25 23:26:26

标签: javascript angularjs plunker

我不明白为什么这个简单的例子在Plunker中不起作用。

http://plnkr.co/edit/EfNxzzQhAb8xAcFZGKm3?p=preview

var app = angular.module("App",[]);

var Controller = function($scope){

  $scope.message ="Hello";
};


app.contoller("Controller",['$scope',Controller]);


var app = angular.module("App",[]);

var Controller = function($scope){

  $scope.message ="Hello";
};


app.contoller("Controller",['$scope',Controller]);

3 个答案:

答案 0 :(得分:4)

Plunker提供开箱即用的角度语法,帮助您开始使用最佳实践:

检查出来:

http://plnkr.co/edit/tpl:8rFfZljYNl3z1A4LKSL2?p=preview

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

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.0-rc.2/angular.js" data-semver="1.4.0-rc.2"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
  </body>

</html>

JS:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

答案 1 :(得分:3)

您将controller拼错为contoller

答案 2 :(得分:2)

这是一个简单的示例,可以帮助您开始使用&#34; Controller as&#34; 语法,这是您应该使用的内容。您将看到许多 $ scope 语法的示例,但它不再是将控制器绑定到视图的推荐方法。有关详细信息,请参阅AngularJS.org's documentation regarding ngController

Plunker:http://plnkr.co/edit/aydvXaJNXtzdVI1mh2I4?p=preview

HTML:

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

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.0-rc.2/angular.js" data-semver="1.4.0-rc.2"></script>
    <script src="app.js"></script>
  </head>

  <body >
    <p ng-controller="MainController as main">Hello {{main.name}}!</p>
    <p ng-controller="BillyGoatController as billyGoat">Hello {{billyGoat.name}}!</p>
  </body>

</html>

JS:

var app = angular.module('controllerAsDemo', []);

app.controller('MainController', function() {
  this.name = 'World';
});

app.controller('BillyGoatController', function() {
  this.name = 'Billy Goat Gruff';
})