直接在index.html中

时间:2016-11-20 14:51:57

标签: javascript angularjs

我需要创建一个单页应用程序,而不需要任何其他文件,如app.js或controller.js

我创建了下面的示例,但是我收到错误: Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title> JavaScript Angular</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js">

        var myАpp = angular.module("myАpp", []);

        myАpp.controller("firstController", function($scope){
            $scope.test = "test-to-test";

        });

    </script>
</head>
<body ng-app = "myApp">
    <div ng-controller = "firstController">
        {{test}}
    </div>

</body>
</html>

那么,是否可以只使用一个带有Angular的文件index.html并且内部仍然有一个控制器?

1 个答案:

答案 0 :(得分:3)

您需要在<script>标记内包含控制器和模块

<div ng-app="myАpp">
  <div ng-controller="firstController">
    <h1>{{test}}</h1>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script>
var app = 
    angular.module('myАpp', []).controller('firstController', function($scope){
  $scope.test  =  "test-to-test";
});
</script>