tri___ton tri___ton - 15 days ago 7
AngularJS Question

include angular controller in index.html

Hi i need to make one page app without additional files like app.js and controller.js

so i do like this and recieve 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.

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


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

Answer

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>

Comments