简体   繁体   English

添加 ng-controller 会停止 AngularJS

[英]Adding ng-controller stops AngularJS

With the below HTML AngularJS is invoked and the webpage prints "5" as the result of {{10/2}} in the "body".调用下面的 HTML AngularJS 并且网页打印“5”作为“body”中 {{10/2}} 的结果。

<!DOCTYPE html>
<html ng-app>
<head>
    <title>AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
    <script src="script.js"></script>
</head>

<body>
    <h2>hello angular</h2>
    {{ 10/2 }}
</body>
</html>

When I add an ng-controller:当我添加一个 ng-controller 时:

<!DOCTYPE html>
<html ng-app>
<head>
    <title>AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
    <script src="script.js"></script>
</head>

<body ng-controller="MainController">
    <h2>hello angular</h2>
    {{ 10/2 }}
</body>
</html>

with the script.js:使用 script.js:

var MainController = function($scope){

    $scope.message = "Hello!"
}

AngularJS stops working and the webpage prints {{10/2}} instead of 5. What am I doing wrong here? AngularJS 停止工作,网页打印 {{10/2}} 而不是 5。我在这里做错了什么? Thanks.谢谢。

In script.js , changescript.js ,更改

var MainController = function($scope){

    $scope.message = "Hello!"
}

to

app.controller('MainController', function($scope) {

  $scope.message = "Hello!"

});

Working Plunker工作Plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM