繁体   English   中英

添加 ng-controller 会停止 AngularJS

[英]Adding ng-controller stops AngularJS

调用下面的 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>

当我添加一个 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>

使用 script.js:

var MainController = function($scope){

    $scope.message = "Hello!"
}

AngularJS 停止工作,网页打印 {{10/2}} 而不是 5。我在这里做错了什么? 谢谢。

script.js ,更改

var MainController = function($scope){

    $scope.message = "Hello!"
}

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

  $scope.message = "Hello!"

});

工作Plunker

暂无
暂无

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

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