1

I need to make a one page app without any additional files like app.js or controller.js

I created the example below, but I get the error: 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>

So, is it possible to have only one file index.html with Angular and still have a controller inside?

2
  • A simple exampe: w3schools.com/angular/tryit.asp?filename=try_ng_controller Commented Nov 20, 2016 at 14:56
  • However, the real reason that your's didn't work is that you had a non ASCII character in your myApp variable. Correcting that, and adjusting the script tag as below worked for me. Commented Nov 20, 2016 at 15:16

1 Answer 1

4

You need to have the controller and module inside the <script> tag

<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>

Sign up to request clarification or add additional context in comments.

1 Comment

I guess you meant firstController should be testController

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.