这个Angularjs代码有什么问题?

时间:2015-03-22 15:32:30

标签: angularjs meanjs

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Simple angular</title>
    <script src="lib/angular/angular.min.js"></script>
</head>
<body>
    <div ng-controller="MyController">

        <h1>{{a.name}}</h1>

    </div>

    <script>
    function MyController($scope) {
        $scope.a = {
            'name':"fox",
            'title':'hello'
        }
    }

    </script>

</body>
</html>
  • 我正在学习angularjs。你能告诉我这段代码有什么问题吗?
  • 我将输出作为&#34; a.name&#34;而不是&#39; fox&#39;

1 个答案:

答案 0 :(得分:5)

由于您正在使用Angular 1.3,因此您需要&#34;附加&#34;你的控制器到你的模块:

<!DOCTYPE html>

<html lang="en" ng-app="myApp"> //Change #1: need to write your app here

<head>
    <meta charset="UTF-8">
    <title>Simple angular</title>
    <script src="lib/angular/angular.min.js"></script>
</head>
<body>
    <div ng-controller="MyController">

        <h1>{{a.name}}</h1>

    </div>

    <script>
    var app = angular.module("myApp", []); //Change #2: declare your app

    function MyController($scope) {
        $scope.a = {
        'name':"fox",
        'title':'hello'
       }
    }

    app.controller("MyController", MyController); //Change #3: attach your controller

    </script>

</body>
</html>

您的代码需要进行3次调整。